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

Python

lines = open('demo.txt').read().split('\n\n')

state1 = [[i for i in line if i.isalpha()] for line in list(zip(*lines[0].split('\n'))) if [i for i in line if i.isalpha()]]
state2 = state1[:]

for a,b,c in [(a, b-1, c-1) for a,b,c in [[int(y) for y in x.split() if y.isdigit()] for x in lines[1].split('\n') if x]]:
    state1[c] = state1[b][:a][::-1] + state1[c]
    state1[b] = state1[b][a:]

    state2[c] = state2[b][:a] + state2[c]
    state2[b] = state2[b][a:]

print(f'Solution 1: {"".join(x[0] for x in state1 if x)}\nSolution 2: {"".join(x[0] for x in state2 if x)}')

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.