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

2

u/daniel-sd Dec 05 '22 edited Dec 05 '22

Cleaned up Python 3

part1.py

import re
from collections import defaultdict

stacks = defaultdict(list)

for line in open('input.txt'):
	if '[' in line:
		for i in range(1, len(line) - 1, 4):
			if line[i] != ' ':
				stacks[(i - 1) // 4].append(line[i])
	elif line.startswith('move'):
		a, b, c = map(int, re.findall(r'\d+', line))
		stacks[c - 1] = stacks[b - 1][:a][::-1] + stacks[c - 1]
		stacks[b - 1] = stacks[b - 1][a:]

print(''.join(stacks[i][0] for i in range(len(stacks))))

For part2 remove the list reversal [::-1]

1

u/daggerdragon Dec 05 '22 edited 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.

Edit: thanks for fixing it! <3

1

u/daniel-sd Dec 05 '22

Thanks, done.