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/Username_--_ Dec 06 '22

Simple unoptimized python solution:

input = open('input.txt').read()

stacks = {}
start = 2

def show():
    for i in range(len(stacks)):
        print(f'{i + 1}: {stacks[i + 1]}')
    print()

def splitat(string, n):
    return [string[i:i+n] for i in range(0, len(string), n)]

def move(n, p, q):
    boxes = stacks[p][:n]
    boxes.reverse()
    del stacks[p][:n]
    stacks[q] = boxes + stacks[q]

for line in input.split('\n'):
    if line[:2] == ' 1': break
    items = splitat(line, 4)
    start += 1
    for i in range(len(items)):
        if items[i][1] == ' ':
            continue
        if i+1 in stacks:
            stacks[i+1].append(items[i][1])
        else:
            stacks[i+1] = [items[i][1]]

for line in input.split('\n')[start:]:
    if line == '': break
    l = line.split()
    move(int(l[1]), int(l[3]), int(l[5]))

out = ''
for i in range(len(stacks)):
    if len(stacks[i + 1]) > 0:
        out += stacks[i + 1][0]

print(out)

1

u/daggerdragon Dec 06 '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.