day 05
supply stacks
approach
the drawing is parsed from the bottom up. the last line, with the stack numbers, gives the column index of each stack, and every line above it is read at those columns, pushing crates onto lists. the moves are three numbers pulled out with a regex.
a move pops k crates off the source and pushes them onto the destination. part 1 moves them one at a time, so they land reversed. part 2 keeps their order, which the code does by reversing the popped batch before pushing it. the answer is the top crate of each stack. the rust version is a port.
the trick
moving a batch at once is the same as moving one at a time and then reversing the batch.
python
def step(state, k, src, dst, preserve):
inter = [state[src].pop() for _ in range(k)]
if preserve:
inter.reverse()
state[dst].extend(inter)
return state