Day 05 – Moving Crates
The Problem Part 1:
The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged.
The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.
The Elves don’t want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.
They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input).
After the rearrangement procedure completes, what crate ends up on top of each stack?
And I guessed what Part 2 would be, before it was even revealed:
Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn’t a CrateMover 9000 – it’s a CrateMover 9001.
The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once.
I’m going to throw the sample input in here as well, because getting through this was the hardest part.
[D] [N] [C] [Z] [M] [P] 1 2 3 move 1 from 2 to 1 move 3 from 1 to 3 move 2 from 2 to 1 move 1 from 1 to 2
It’s essentially, two inputs one file. A “drawing” of the stack, and a series of moves. Splitting these up was simple enough, there is a double space, and split() works on “\n\n”. What was hard, was manipulating the Crate Stack drawing into a list of lists so it was easy to work with on the moves function.
I’ll post the whole code, but I want to look at this function, because it was the hardest part and the most interesting thing here.
def pivot(crates): crates.pop(-1) processed =  for row in crates: row = row.replace(' ', ' [ ]') # print(len(row)) while len(row) < len(crates[-1]): row += ' [ ]' row = row[1:-1].split('] [') # print(row) processed.append(row) # print(processed) return list(zip(*processed[::-1]))
What I am looking for, is a list of lists, for each “stack”. The problem is, my input is “vertical” instead of “horizontal” and it has gaps. I considered that there may be a way to do this with Pandas and Data Frames, but a lot of these puzzles really feel like the fun part is solving them without a bunch of complicated imports.
The first step is to remove the bottom row, it’s just column labels, I don’t care about that part.
After a lot of consideration for how to handle the “blanks” I realized the best way was to convert them into “Blank Crates”. Which is where the “row = row.replace(‘ ‘, ‘ [ ]’)” section comes in. I also found that for the pivot later, I would need to fill in any blank spaces at the end of the rows as well, which si where the following while loop comes in.
With the array filled in “fully” with blanks, I can straight split each row on “] [” and get a list, which is appended to “processed” as a fresh rwo, giving me a list of lists, but it’s still veetical. I went out looking for a “pivot” function. For anyone not familiar with this term, a super layman explanation is, a Pivot Table, is essentially a new table, based on values of an old table. It’s been ‘Pivoted”. A list of lists, is essentiallyy just “a table” of data, A series of rows and columns. All I want to do is literally pivot it so X becomes Y, because it makes things easier to “move crates” later with split() and pop() functions. What I found was this:
That handy Zip function again, which will basically just, do what I want.
The only other real problem I had with this puzzle was on Part 2. I tried several methods I found online, but I could not get a second copy of my original list. It seems Python does some “Screwy Bullshit” with it’s lists where copies are not copies. I kept getting errors trying to run my Part 2 code, index out of range. Well, it was starting with the modified version of the original crates list. I tried crates2 = crates[:], and crates2 = crates.copy() and crates2 = copy.copy() and nothing actually did what I needed.
So I just, commented out the Part 1 code so it wouldn’t run.
with open("Day05Input.txt") as file: data = file.read() split_data = data.split('\n\n') crates = split_data.split('\n') moves = split_data.split('\n') def pivot(crates): crates.pop(-1) processed =  for row in crates: row = row.replace(' ', ' [ ]') # print(len(row)) while len(row) < len(crates[-1]): row += ' [ ]' row = row[1:-1].split('] [') # print(row) processed.append(row) # print(processed) return list(zip(*processed[::-1])) def drop_blanks(crates): de_blanked =  for crate in crates: row = [i for i in crate if i != ' '] de_blanked.append(row) return de_blanked def move_crates(crate_move, moves): for move in moves: steps = move.split(" ") how_many = int(steps) from_stack = int(steps)-1 to_stack = int(steps)-1 for i in range(0,how_many): # print(crate_move[from_stack]) # print(crate_move[to_stack]) crate_move[to_stack].append(crate_move[from_stack].pop()) #print(steps) return crate_move def move_crates_in_stacks(crates_stack, moves): for move in moves: steps = move.split(" ") how_many = int(steps) from_stack = int(steps)-1 to_stack = int(steps)-1 #print(crates_stack[from_stack]) move_list = crates_stack[from_stack][-how_many:] #print(crates_stack[to_stack]) for i in range(how_many): crates_stack[from_stack].pop() crates_stack[to_stack].append(move_list[i]) #print(steps) return crates_stack crates = pivot(crates) crates = drop_blanks(crates) #Part 1 #moved = move_crates(crates, moves) moved2 = move_crates_in_stacks(crates, moves) print(crates) #print(len(crates)) #print(moves) #Part 1 # for each in moved: # print(each[-1]) #svfdLGLWV for each in moved2: print(each[-1]) #DCVTCVPCL