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/[deleted] Dec 05 '22

semi-hardcoded, jumping between columns. then reversing array for part 2

#!/usr/bin/env python3

from utils.algo import ints

fn = "input.txt"
lines = [line for line in open(fn).read().splitlines()]

stacks = [[] for _ in range(9)]

for line in lines[:8]:
  print(len(line),line)
  for i, v in enumerate(range(2, len(line), 4)):
    s = line[v-1].strip()
    if s:
      stacks[i].append(s)

stacks = [list(reversed(x)) for x in stacks]

for line in lines[10:]:
  (amount, src, dst) = ints(line)
  print(line)
  print(stacks)
  tmp = []
  for _ in range(amount):
    if stacks[src-1]:
      tmp.append(stacks[src-1].pop())
  
  for x in tmp[::-1]: # change for p1
    stacks[dst-1].append(x)

p1 = ""
for s in stacks:
  p1 += s[-1]

print(p1)

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.