-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5.py
34 lines (30 loc) · 994 Bytes
/
day_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from collections import deque
import re
with open('inputs/day_5_test.txt', 'r') as f:
line = f.readline()
# one box is len 3, + space between them, without a space at the end of the line
stacks_size = (len(line) + 1) // 4
stacks = [deque() for _ in range(stacks_size)]
def fill_stacks(input_line: str):
for i in range(1, len(input_line), 4):
if input_line[i] != ' ':
stacks[i // 4].appendleft(input_line[i])
while '[' in line:
fill_stacks(line)
line = f.readline()
line = f.readline()
line = f.readline()
def parse_order(input_line: str):
x = re.findall('\d+', input_line)
for i in range(int(x[0])):
fr = int(x[1]) - 1
to = int(x[2]) - 1
block = stacks[fr].pop()
stacks[to].append(block)
while line:
parse_order(line)
line = f.readline()
answer = ''
for stack in stacks:
answer += stack.pop()
print(answer)