52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
# 2022 - Advent Of Code 5 part 2
|
|
|
|
# file = 'input_example.txt'
|
|
file = 'input.txt'
|
|
|
|
parsed_stack = []
|
|
num_crates = 0 # num crates by line
|
|
moves = []
|
|
with open(file, encoding="utf-8") as f:
|
|
while True:
|
|
line = f.readline()
|
|
if line[1] == '1':
|
|
break
|
|
num_crates = int(len(line)/4)
|
|
crates = []
|
|
for crate in range(0, num_crates):
|
|
letter = line[1 + crate * 4]
|
|
crates.append(letter)
|
|
parsed_stack.append(crates)
|
|
line = f.readline()
|
|
for line in f.readlines():
|
|
split = line.strip().split(' ')
|
|
moves.append((int(split[1]), int(split[3]), int(split[5])))
|
|
|
|
print(f'moves: {moves}')
|
|
print(f'num crates by line: {num_crates}')
|
|
# print(parsed_stack)
|
|
print(f'init stack height: {len(parsed_stack)}')
|
|
stacks = []
|
|
|
|
for s in range(0, num_crates):
|
|
stack = []
|
|
for h in range(len(parsed_stack)-1, -1, -1):
|
|
if parsed_stack[h][s] == ' ':
|
|
break
|
|
stack.append(parsed_stack[h][s])
|
|
stacks.append(stack)
|
|
|
|
for s in stacks:
|
|
print(s)
|
|
|
|
for move in moves:
|
|
num_move = move[0]
|
|
from_stack = stacks[move[1] - 1]
|
|
dest_stack = stacks[move[2] - 1]
|
|
dest_stack.extend(from_stack[-num_move:])
|
|
del from_stack[-num_move:]
|
|
|
|
print(stacks)
|
|
result = ''.join([s[-1] for s in stacks])
|
|
print(result)
|