Definitely a bit more involved for me.

master
Ben Maurer 2022-12-07 14:34:43 -06:00
parent f6d828c1a1
commit 1070f5197e
1 changed files with 27 additions and 0 deletions

27
day_5.py 100644
View File

@ -0,0 +1,27 @@
import aocd
from dotenv import load_dotenv
load_dotenv()
puzzle_input = aocd.get_data(day=5, year=2022)
inputs = puzzle_input.split('\n\n')
cargo = [[],[],[],[],[],[],[],[],[]]
moves = inputs[1].replace('move ','').replace(' from ',',').replace(' to ',',').split('\n')
transposed_cargo = inputs[0].split('\n')[:-1]
for s,stack in enumerate(cargo):
for transposed_stack in reversed(transposed_cargo):
for letter in transposed_stack[1+s*4]:
if letter != ' ':
stack.append(letter)
for m,move in enumerate(moves):
column_to = int(move.split(',')[2])-1
column_from = int(move.split(',')[1])-1
moved_number = int(move.split(',')[0])
cargo[column_to] = cargo[column_to] + list(reversed(cargo[column_from][-moved_number::]))
cargo[column_from] = cargo[column_from][:-moved_number]
answer = []
for stack in cargo:
answer.append(stack[-1])
print(str("".join(answer)))