This commit is contained in:
parent
a37a55a9d4
commit
f8c9bcea54
1 changed files with 52 additions and 0 deletions
52
2022/5/5_2.py
Normal file
52
2022/5/5_2.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
#!/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)
|
Loading…
Reference in a new issue