From 81cee6f9d90848cdc213fcb5ed3fc2cffd37a2a7 Mon Sep 17 00:00:00 2001 From: Luc Barrett Date: Mon, 5 Dec 2022 00:54:08 -0500 Subject: [PATCH] Day 5 --- day5.py | 88 ++++++++ day5.txt | 512 ++++++++++++++++++++++++++++++++++++++++++ tempCodeRunnerFile.py | 2 + 3 files changed, 602 insertions(+) create mode 100644 day5.py create mode 100644 day5.txt create mode 100644 tempCodeRunnerFile.py diff --git a/day5.py b/day5.py new file mode 100644 index 0000000..c072d16 --- /dev/null +++ b/day5.py @@ -0,0 +1,88 @@ +import numpy as np + +""" +Advent of Code 2022 - Day 5 + +Notes: +wow, lot of code this time. Parsing could be cut down quite ab it using divisions of the line length, +and later with regex. Probably worth praciticing regex more. Faster solutions used a lot of clever slicing +too. + +Def need to work on more compact parsing, this would be good input data for practice + +Time taken: ~20 min + +""" + +with open("day5.txt", "r") as file: + inp = file.read().split("\n") + +def getLetters(line): + """ + From a line, extract the letters from the line + returns in format ["", "", "", "A", "B"], where "" are empty spots + + """ + letters = [] + spl = line.replace(" ", "-").replace(" ", "-").split("-") + for item in spl: + if item == "": + letters.append("") + elif item[0] == "[": + letters.append(item[1]) + else: + pass + return letters + + +table = [] +inst_index = 0 +for i, line in enumerate(inp): + table.append(getLetters(line)) #append to table the letters + if len(line) == 0: #check for when weve reached the end of the table + del table[-1] #and remove the extra entries + del table[-1] + inst_index = i+1 #record the index the instructions begin + break + +table = np.transpose(table) #transpose to get it to be a list of stacks +stacks = [] # here I realized how convienent stack data structures would be, so quick + # reorganization of data into stacks without blank spaces + # would've been better to do this from the start but oh well +for i, stack in enumerate(table): + stacks.append([]) + for item in stack: + if item != "": + stacks[i].append(item) + stacks[i].reverse() #since we append from top down, reverse so our top elements are at the + #end of the stack + +#part 2 +def makeMove_p2(n, s1, s2): #make a move, move n crates from s1 to s2, order maintained + int_stack = [] + for i in range(0, n): + int_stack.append(stacks[s1-1].pop()) + for i in range(0, n): + stacks[s2-1].append(int_stack.pop()) + +#part 1 +def makeMove_p1(n, s1, s2): #make a move, move n crates from s1 to s2, order not maintaned + for i in range(0, n): + stacks[s2-1].append(stacks[s1-1].pop()) + + +for i in range(inst_index, len(inp)): #carry out move for each instruction + line = inp[i] + line = line.split(" ") + n = int(line[1] ) + s1 = int(line[3]) + s2 = int(line[5]) + + makeMove_p2(n, s1, s2) + +#print top item for each stack +for stack in stacks: + print(stack.pop(), end="") + + + diff --git a/day5.txt b/day5.txt new file mode 100644 index 0000000..8e9087d --- /dev/null +++ b/day5.txt @@ -0,0 +1,512 @@ + [M] [Z] [V] + [Z] [P] [L] [Z] [J] +[S] [D] [W] [W] [H] [Q] +[P] [V] [N] [D] [P] [C] [V] +[H] [B] [J] [V] [B] [M] [N] [P] +[V] [F] [L] [Z] [C] [S] [P] [S] [G] +[F] [J] [M] [G] [R] [R] [H] [R] [L] +[G] [G] [G] [N] [V] [V] [T] [Q] [F] + 1 2 3 4 5 6 7 8 9 + +move 6 from 9 to 3 +move 2 from 2 to 1 +move 1 from 8 to 2 +move 3 from 7 to 2 +move 7 from 6 to 9 +move 1 from 9 to 5 +move 3 from 5 to 7 +move 6 from 8 to 6 +move 1 from 7 to 8 +move 6 from 6 to 5 +move 4 from 5 to 8 +move 9 from 2 to 9 +move 1 from 2 to 3 +move 3 from 1 to 3 +move 3 from 5 to 1 +move 10 from 3 to 5 +move 4 from 4 to 6 +move 2 from 7 to 6 +move 2 from 6 to 9 +move 6 from 8 to 6 +move 1 from 4 to 3 +move 1 from 4 to 5 +move 1 from 4 to 1 +move 2 from 3 to 1 +move 1 from 3 to 7 +move 8 from 1 to 9 +move 1 from 1 to 2 +move 1 from 2 to 7 +move 6 from 6 to 3 +move 7 from 3 to 5 +move 14 from 5 to 6 +move 2 from 1 to 3 +move 5 from 5 to 8 +move 5 from 8 to 1 +move 2 from 7 to 1 +move 5 from 6 to 9 +move 8 from 9 to 3 +move 13 from 9 to 3 +move 7 from 1 to 4 +move 6 from 4 to 1 +move 22 from 3 to 1 +move 1 from 9 to 3 +move 2 from 6 to 1 +move 1 from 3 to 4 +move 7 from 9 to 8 +move 2 from 1 to 7 +move 2 from 3 to 2 +move 2 from 6 to 9 +move 2 from 7 to 8 +move 1 from 3 to 6 +move 9 from 8 to 6 +move 1 from 2 to 4 +move 8 from 1 to 2 +move 1 from 9 to 4 +move 3 from 4 to 1 +move 1 from 4 to 6 +move 10 from 6 to 5 +move 5 from 2 to 9 +move 6 from 9 to 3 +move 2 from 5 to 3 +move 2 from 9 to 7 +move 7 from 5 to 8 +move 5 from 6 to 2 +move 3 from 3 to 7 +move 3 from 3 to 5 +move 4 from 5 to 8 +move 1 from 3 to 5 +move 6 from 6 to 8 +move 1 from 5 to 7 +move 9 from 8 to 9 +move 1 from 3 to 1 +move 7 from 2 to 7 +move 9 from 7 to 6 +move 2 from 2 to 3 +move 7 from 9 to 3 +move 9 from 6 to 8 +move 7 from 3 to 4 +move 2 from 7 to 6 +move 4 from 4 to 5 +move 3 from 5 to 6 +move 2 from 7 to 4 +move 5 from 4 to 7 +move 13 from 8 to 4 +move 2 from 9 to 4 +move 2 from 8 to 7 +move 6 from 7 to 5 +move 6 from 4 to 2 +move 1 from 7 to 5 +move 3 from 2 to 7 +move 1 from 7 to 8 +move 3 from 2 to 4 +move 2 from 3 to 9 +move 2 from 7 to 2 +move 6 from 5 to 4 +move 3 from 6 to 2 +move 2 from 6 to 9 +move 5 from 2 to 9 +move 12 from 4 to 8 +move 3 from 9 to 2 +move 12 from 1 to 5 +move 4 from 4 to 6 +move 12 from 8 to 9 +move 2 from 6 to 5 +move 1 from 4 to 8 +move 1 from 4 to 1 +move 3 from 2 to 1 +move 2 from 6 to 7 +move 1 from 5 to 9 +move 2 from 1 to 4 +move 10 from 5 to 1 +move 2 from 7 to 3 +move 18 from 9 to 7 +move 8 from 7 to 2 +move 1 from 9 to 6 +move 1 from 6 to 7 +move 10 from 7 to 9 +move 1 from 4 to 2 +move 19 from 1 to 5 +move 8 from 5 to 9 +move 3 from 8 to 4 +move 2 from 5 to 2 +move 2 from 3 to 6 +move 10 from 5 to 2 +move 4 from 1 to 2 +move 2 from 9 to 2 +move 1 from 1 to 6 +move 2 from 5 to 6 +move 1 from 8 to 7 +move 1 from 5 to 8 +move 1 from 5 to 6 +move 18 from 2 to 5 +move 5 from 2 to 1 +move 6 from 5 to 8 +move 1 from 8 to 9 +move 2 from 2 to 4 +move 1 from 2 to 6 +move 2 from 7 to 6 +move 1 from 4 to 1 +move 4 from 8 to 5 +move 1 from 2 to 9 +move 2 from 8 to 3 +move 1 from 3 to 6 +move 1 from 4 to 8 +move 1 from 8 to 9 +move 10 from 5 to 7 +move 5 from 5 to 1 +move 2 from 4 to 1 +move 3 from 7 to 6 +move 12 from 1 to 4 +move 8 from 9 to 5 +move 6 from 7 to 4 +move 1 from 7 to 9 +move 4 from 4 to 3 +move 1 from 1 to 7 +move 3 from 9 to 5 +move 2 from 3 to 1 +move 1 from 7 to 6 +move 8 from 4 to 7 +move 1 from 7 to 6 +move 7 from 6 to 4 +move 2 from 1 to 3 +move 1 from 7 to 1 +move 1 from 3 to 7 +move 1 from 1 to 6 +move 4 from 9 to 3 +move 5 from 4 to 6 +move 12 from 6 to 2 +move 3 from 9 to 4 +move 8 from 2 to 6 +move 2 from 9 to 6 +move 8 from 5 to 6 +move 4 from 5 to 8 +move 14 from 6 to 3 +move 11 from 4 to 9 +move 2 from 2 to 7 +move 8 from 3 to 9 +move 11 from 3 to 6 +move 14 from 9 to 1 +move 7 from 1 to 3 +move 2 from 9 to 5 +move 2 from 2 to 8 +move 6 from 7 to 5 +move 1 from 9 to 8 +move 13 from 6 to 3 +move 4 from 6 to 8 +move 3 from 1 to 6 +move 5 from 5 to 8 +move 7 from 8 to 7 +move 2 from 1 to 8 +move 1 from 4 to 1 +move 4 from 8 to 9 +move 8 from 7 to 5 +move 1 from 8 to 1 +move 4 from 9 to 3 +move 1 from 4 to 5 +move 5 from 5 to 2 +move 1 from 8 to 9 +move 1 from 8 to 6 +move 2 from 6 to 2 +move 4 from 8 to 6 +move 4 from 1 to 8 +move 4 from 8 to 5 +move 1 from 9 to 8 +move 1 from 2 to 3 +move 4 from 6 to 1 +move 1 from 8 to 2 +move 3 from 5 to 4 +move 4 from 2 to 5 +move 1 from 7 to 9 +move 1 from 2 to 6 +move 3 from 1 to 8 +move 2 from 4 to 5 +move 2 from 6 to 1 +move 3 from 8 to 9 +move 4 from 9 to 2 +move 1 from 7 to 1 +move 1 from 6 to 7 +move 4 from 1 to 6 +move 1 from 7 to 4 +move 6 from 2 to 8 +move 2 from 4 to 8 +move 1 from 9 to 5 +move 3 from 6 to 2 +move 1 from 6 to 4 +move 7 from 3 to 5 +move 2 from 8 to 1 +move 3 from 2 to 8 +move 6 from 8 to 5 +move 17 from 5 to 3 +move 2 from 1 to 6 +move 3 from 8 to 3 +move 1 from 9 to 5 +move 11 from 5 to 2 +move 40 from 3 to 5 +move 11 from 2 to 7 +move 4 from 7 to 8 +move 1 from 8 to 9 +move 1 from 3 to 5 +move 1 from 4 to 8 +move 19 from 5 to 8 +move 7 from 7 to 8 +move 16 from 5 to 2 +move 6 from 5 to 8 +move 1 from 5 to 8 +move 1 from 9 to 4 +move 1 from 6 to 1 +move 1 from 4 to 7 +move 1 from 6 to 9 +move 1 from 1 to 7 +move 1 from 7 to 3 +move 1 from 7 to 2 +move 1 from 9 to 8 +move 1 from 3 to 4 +move 1 from 4 to 6 +move 14 from 2 to 9 +move 24 from 8 to 4 +move 8 from 8 to 3 +move 1 from 6 to 3 +move 16 from 4 to 1 +move 3 from 8 to 4 +move 3 from 3 to 8 +move 4 from 3 to 4 +move 1 from 3 to 9 +move 13 from 9 to 4 +move 16 from 1 to 8 +move 8 from 8 to 1 +move 3 from 1 to 7 +move 1 from 8 to 6 +move 1 from 3 to 8 +move 10 from 8 to 5 +move 5 from 5 to 2 +move 3 from 8 to 9 +move 1 from 8 to 9 +move 1 from 4 to 5 +move 5 from 2 to 6 +move 3 from 5 to 2 +move 1 from 6 to 1 +move 5 from 1 to 5 +move 1 from 1 to 5 +move 2 from 7 to 3 +move 2 from 3 to 2 +move 1 from 5 to 7 +move 7 from 5 to 3 +move 5 from 9 to 5 +move 2 from 7 to 9 +move 4 from 5 to 6 +move 2 from 9 to 8 +move 2 from 2 to 4 +move 5 from 3 to 5 +move 1 from 3 to 2 +move 7 from 4 to 9 +move 1 from 8 to 1 +move 1 from 2 to 1 +move 9 from 4 to 6 +move 2 from 1 to 8 +move 1 from 3 to 9 +move 2 from 8 to 6 +move 13 from 4 to 6 +move 1 from 8 to 7 +move 2 from 9 to 6 +move 3 from 5 to 7 +move 3 from 2 to 5 +move 3 from 2 to 6 +move 5 from 6 to 2 +move 4 from 2 to 5 +move 4 from 5 to 7 +move 5 from 5 to 7 +move 7 from 9 to 6 +move 6 from 7 to 2 +move 22 from 6 to 5 +move 10 from 5 to 8 +move 7 from 5 to 4 +move 8 from 8 to 5 +move 18 from 6 to 2 +move 5 from 7 to 5 +move 1 from 8 to 2 +move 6 from 5 to 1 +move 7 from 4 to 2 +move 4 from 1 to 5 +move 1 from 7 to 9 +move 1 from 8 to 6 +move 1 from 7 to 8 +move 10 from 5 to 9 +move 12 from 2 to 1 +move 8 from 5 to 2 +move 19 from 2 to 9 +move 1 from 6 to 8 +move 13 from 9 to 3 +move 8 from 1 to 2 +move 5 from 1 to 3 +move 10 from 2 to 1 +move 7 from 2 to 5 +move 3 from 5 to 7 +move 4 from 1 to 3 +move 1 from 2 to 3 +move 3 from 1 to 2 +move 1 from 8 to 6 +move 2 from 7 to 5 +move 4 from 1 to 3 +move 6 from 5 to 4 +move 2 from 2 to 1 +move 1 from 2 to 9 +move 6 from 4 to 5 +move 5 from 5 to 9 +move 1 from 6 to 8 +move 1 from 5 to 1 +move 6 from 9 to 2 +move 5 from 2 to 4 +move 3 from 1 to 6 +move 2 from 4 to 7 +move 22 from 3 to 9 +move 1 from 8 to 4 +move 2 from 4 to 3 +move 2 from 6 to 1 +move 2 from 1 to 5 +move 1 from 6 to 7 +move 1 from 7 to 4 +move 6 from 3 to 7 +move 1 from 2 to 4 +move 8 from 7 to 3 +move 1 from 4 to 5 +move 1 from 7 to 9 +move 5 from 3 to 6 +move 1 from 8 to 4 +move 4 from 3 to 2 +move 32 from 9 to 3 +move 3 from 6 to 7 +move 5 from 9 to 3 +move 1 from 9 to 7 +move 2 from 9 to 2 +move 2 from 4 to 3 +move 2 from 5 to 4 +move 5 from 3 to 2 +move 3 from 7 to 8 +move 1 from 7 to 2 +move 1 from 8 to 5 +move 1 from 3 to 4 +move 5 from 4 to 5 +move 4 from 5 to 2 +move 3 from 5 to 7 +move 1 from 7 to 5 +move 1 from 6 to 5 +move 2 from 8 to 5 +move 15 from 2 to 4 +move 3 from 5 to 6 +move 4 from 6 to 5 +move 2 from 5 to 2 +move 1 from 2 to 4 +move 25 from 3 to 9 +move 2 from 5 to 2 +move 11 from 9 to 2 +move 13 from 2 to 1 +move 4 from 4 to 7 +move 12 from 9 to 8 +move 6 from 7 to 8 +move 7 from 4 to 7 +move 7 from 7 to 8 +move 1 from 5 to 1 +move 5 from 4 to 3 +move 2 from 2 to 1 +move 2 from 9 to 5 +move 7 from 1 to 7 +move 1 from 1 to 4 +move 12 from 3 to 2 +move 1 from 3 to 9 +move 1 from 1 to 3 +move 1 from 9 to 1 +move 7 from 7 to 2 +move 1 from 4 to 7 +move 2 from 8 to 7 +move 7 from 1 to 2 +move 1 from 3 to 4 +move 26 from 2 to 1 +move 4 from 8 to 1 +move 3 from 1 to 6 +move 1 from 6 to 3 +move 1 from 6 to 9 +move 1 from 3 to 8 +move 20 from 1 to 3 +move 1 from 9 to 7 +move 4 from 7 to 1 +move 1 from 5 to 3 +move 4 from 3 to 5 +move 1 from 6 to 2 +move 6 from 3 to 2 +move 8 from 1 to 4 +move 1 from 1 to 5 +move 3 from 1 to 4 +move 7 from 2 to 4 +move 10 from 3 to 8 +move 4 from 4 to 3 +move 12 from 4 to 7 +move 3 from 3 to 1 +move 2 from 4 to 3 +move 2 from 8 to 1 +move 6 from 8 to 9 +move 5 from 9 to 6 +move 1 from 9 to 3 +move 3 from 8 to 7 +move 10 from 8 to 5 +move 4 from 8 to 7 +move 9 from 7 to 9 +move 4 from 8 to 4 +move 2 from 4 to 3 +move 3 from 1 to 7 +move 11 from 7 to 4 +move 6 from 4 to 8 +move 1 from 7 to 3 +move 4 from 5 to 1 +move 5 from 3 to 6 +move 5 from 9 to 4 +move 1 from 9 to 8 +move 10 from 4 to 8 +move 5 from 1 to 2 +move 1 from 7 to 6 +move 9 from 6 to 3 +move 7 from 8 to 7 +move 3 from 4 to 1 +move 2 from 2 to 1 +move 9 from 8 to 3 +move 10 from 5 to 8 +move 18 from 3 to 9 +move 1 from 7 to 8 +move 1 from 5 to 3 +move 4 from 8 to 3 +move 2 from 6 to 3 +move 6 from 7 to 2 +move 1 from 5 to 3 +move 1 from 1 to 9 +move 10 from 3 to 9 +move 4 from 1 to 8 +move 13 from 8 to 1 +move 3 from 1 to 8 +move 3 from 2 to 4 +move 5 from 2 to 6 +move 5 from 6 to 4 +move 28 from 9 to 2 +move 2 from 9 to 5 +move 2 from 5 to 2 +move 1 from 3 to 7 +move 2 from 1 to 4 +move 3 from 8 to 3 +move 1 from 9 to 4 +move 3 from 4 to 6 +move 2 from 3 to 7 +move 8 from 1 to 5 +move 3 from 7 to 6 +move 14 from 2 to 8 +move 1 from 9 to 1 +move 6 from 5 to 6 +move 4 from 2 to 5 +move 9 from 8 to 2 +move 4 from 8 to 4 +move 7 from 2 to 4 +move 12 from 4 to 3 +move 5 from 4 to 7 +move 5 from 7 to 4 +move 1 from 8 to 7 +move 1 from 4 to 5 +move 2 from 5 to 4 +move 1 from 5 to 8 +move 1 from 5 to 9 \ No newline at end of file diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 0000000..87c58ea --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1,2 @@ + +print(input)