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). For example:
[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
In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z
is on the bottom, and crate N
is on top. Stack 2 contains three crates; from bottom to top, they are crates M
, C
, and D
. Finally, stack 3 contains a single crate, P
.
Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D
) ends up below the second and third crates:
[Z]
[N]
[C] [D]
[M] [P]
1 2 3
Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C
ends up below crate M
:
[Z]
[N]
[M] [D]
[C] [P]
1 2 3
Finally, one crate is moved from stack 1 to stack 2:
[Z]
[N]
[D]
[C] [M] [P]
1 2 3
The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C
in stack 1, M
in stack 2, and Z
in stack 3, so you should combine these together and give the Elves the message CMZ
.
After the rearrangement procedure completes, what crate ends up on top of each stack?
# Python imports
from pathlib import Path
test = Path("data/day05_test.txt") # test data
data = Path("data/day05_data.txt") # puzzle data
def parse_input(fpath: Path) -> tuple[list[list[str]], list[tuple[int, int, int]]]:
"""Parse puzzle input into stack and instruction sets
:param fpath: path to puzzle input
"""
# Parse files
with fpath.open() as ifh:
instructions = [] # list of instructions as (num, from, to) tuples
stacks = [] # list of stacks; each stack is a list and pops from the left
# Parse stacks/instructions
state = "stacks"
for line in [_.rstrip() for _ in ifh.readlines()]:
if 0 == len(line.strip()): # skip blank lines
continue
elif state == "stacks": # if we're parsing stacks...
boxes = [line[idx:idx + 4] for idx in range(0, len(line), 4)]
while len(stacks) < len(boxes): # add new empty stack if required
stacks.append([])
if boxes[0].strip() == "1":
state = "instructions" # Move to parsing instructions
else:
for idx, box in enumerate(boxes): # Add boxes to stacks
if "[" in box:
stacks[idx].append(box)
elif state == "instructions": # if we're parsing instructions ...
instr = line.strip().split()
instructions.append((int(instr[1]), int(instr[3]), int(instr[5]))) # num, from, to
return stacks, instructions
def move_boxes(stacks: list[list[str]], instructions: list[tuple[int, int, int]]) -> list[list[str]]:
"""Return stack arrangement after Cratemover 9000 action
:param stacks: initial arrangement of stacks
:param instructions: instructions for moving boxes around stacks
"""
for num, stfr, stto in instructions:
# Move num boxes from stfr to stto
for _ in range(num):
stacks[stto-1] = [stacks[stfr-1].pop(0)] + stacks[stto-1]
return stacks
def part1(fpath: Path) -> str:
"""Solve the puzzle
:param fpath: path to puzzle input
"""
stacks, instructions = parse_input(fpath) # parse the puzzle input
stacks = move_boxes(stacks, instructions) # move the boxes around the stacks
return ''.join([stack[0][1] for stack in stacks])
part1(test)
'CMZ'
part1(data)
'FWNSHLDNZ'
As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction.
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.
Again considering the example above, the crates begin in the same configuration:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
Moving a single crate from stack 2 to stack 1 behaves the same as before:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C
that gets moved:
[D]
[N]
[Z]
[M] [C] [P]
1 2 3
In this example, the CrateMover 9001 has put the crates in a totally different order: MCD
.
Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack?
def move_boxes_2(stacks: list[list[str]], instructions: list[tuple[int, int, int]]) -> list[list[str]]:
"""Return stack arrangement after Cratemover 9001 action
:param stacks: initial arrangement of stacks
:param instructions: instructions for moving boxes around stacks
"""
for num, stfr, stto in instructions:
# Move num boxes from stfr to stto in one go
stacks[stto-1] = stacks[stfr-1][:num] + stacks[stto-1]
del stacks[stfr-1][:num] # (remove boxes from source stack)
return stacks
def part2(fpath: Path) -> str:
"""Solve the puzzle
:param fpath: path to puzzle input
"""
stacks, instructions = parse_input(fpath) # parse puzzle input
stacks = move_boxes_2(stacks, instructions) # move boxes around stacks
return ''.join([stack[0][1] for stack in stacks])
part2(test)
'MCD'
part2(data)
'RNRGDNFQG'