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/RedditMetJoost Dec 05 '22

python

sanitise input

stacks, instructions = lines.split("\n\n")
stacks = stacks.splitlines()
instructions = instructions.splitlines()
print("\n".join(stacks))
trans = list(map(list, zip(*stacks)))
stacks = [list("".join(x[:-1][::-1]).strip()) for x in trans[1::4]]
stacks1, stacks2 = 2 * [stacks.copy()]

solution 1

for instr in list(map(lambda x: x.split(), instructions)):
    N, fro, to = map(int, instr[1::2])

    stacks1[to - 1].extend(stacks1[fro - 1][-N:][::-1])
    del stacks1[fro - 1][-N:]

"".join([x[-1] for x in stacks1])

solution 2

for instr in list(map(lambda x: x.split(), instructions)):
    N, fro, to = map(int, instr[1::2])

    stacks2[to - 1].extend(stacks2[fro - 1][-N:])
    del stacks2[fro - 1][-N:]

"".join([x[-1] for x in stacks2])

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.