Unable to load image

Advent of Code Day 5, AKA Stacks 101 :marseyinabox:

I actually stayed up to get a semi-decent score for once (being a eurocel for this is suffering). How are you all faring with shifting boxes around?

![](/images/16702184438592093.webp)

26
Jump in the discussion.

No email address required.

Took me almost an hour to figure out how to parse the crates. I was way too tired. I tried putting them into rows and then transposing the whole thing for some reason. Finally tried just reading character by character into a column map.

import re
from collections import deque


with open("./input5.txt") as infile:
    row_text, moves = infile.read().split("\n\n")


stacks = {i:deque() for i in range(1,10)}
mpat = re.compile(r'move (\d+) from (\d+) to (\d+)')


for row in row_text.splitlines():
    for col, char in enumerate(row, 1):
        if char == '[':
            stacks[col // 4 + 1].append(row[col])


for m in map(mpat.search, moves.splitlines()):
    if not m: continue
    a, f, t = map(int, m.groups())
    stack = deque()
    for _ in range(a):
        rune = stacks[f].popleft()
        # part 1
        # stack.append(rune)
        # part 2
        # stack.appendleft(rune)
    for item in stack:
        stacks[t].appendleft(item)
                    
print(''.join(map(deque.popleft, stacks.values())))
Jump in the discussion.

No email address required.

This is the most inefficient code I have ever seen. You should be ashamed of yourself.

Jump in the discussion.

No email address required.



Now playing: Boss Bossanova (DKC2).mp3

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.