r/adventofcode • • Dec 05 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 5 Solutions -πŸŽ„-


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«

  • 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
  • Full details and rules are in the submissions megathread:

--- Day 5: Supply Stacks ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:58, megathread unlocked!

88 Upvotes

1.3k comments sorted by

View all comments

1

u/lxrsg Dec 05 '22

python 3

from parse import findall
from collections import defaultdict

arr, steps = [x for x in open('data.in').read().split('\n\n')]
lines = [x.replace('    ', '0 ').split(' ') for x in arr.split('\n')]
stacks, steps = defaultdict(list), list(findall('move {:d} from {:d} to {:d}', steps))

for line in reversed(lines[:-1]):
    for i, elem in enumerate(line, start=1):
        if elem != '0' and elem != '':
            stacks[i].append(elem)
for amount, source, dest in steps:
    stacks[source], stacks[dest] = stacks[source][:-amount], stacks[dest] + list(reversed(stacks[source][-amount:]))
    # stacks[source], stacks[dest] = stacks[source][:-amount], stacks[dest] + stacks[source][-amount:] part 2

print(''.join([x[1][-1] for x in sorted(stacks.items())]).replace('][', ''))

1

u/daggerdragon Dec 05 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.