Files
aoc/2022/05/e.py
2023-11-30 19:20:34 +02:00

51 lines
1.1 KiB
Python

import os
input = open(os.path.dirname(__file__) +
"/input.txt", "r").read().split('\n\n')
stack_input = input[0].splitlines()
moves = input[1].splitlines()
stacks = []
stack_input.pop()
stack_input.reverse()
for i in [1, 5, 9, 13, 17, 21, 25, 29, 33]:
temp = []
for l in range(0, len(stack_input), 1):
if stack_input[l][i].isalpha():
temp.append(stack_input[l][i])
stacks.append(temp)
stacks2 = [row[:] for row in stacks]
for move in moves:
move = move.split(' ')
amount = int(move[1])
move_from = int(move[3])
move_to = int(move[5])
i = 0
while (i < amount):
i += 1
moved = stacks[move_from-1].pop()
stacks[move_to-1].append(moved)
index = len(stacks2[move_from-1])-amount
tomove = stacks2[move_from-1][index:]
stacks2[move_from-1] = stacks2[move_from-1][:index]
stacks2[move_to-1].extend(tomove)
res = ''
for stack in stacks:
res += stack[len(stack)-1]
print("First answer: " + res)
res2 = ''
for stack in stacks2:
res2 += stack[len(stack)-1]
print("Second answer: " + res2)