-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
74 lines (56 loc) · 1.9 KB
/
script.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import string
def chunk(inputs, chunk_size):
return (inputs[pos:pos + chunk_size]
for pos in range(0, len(inputs), chunk_size))
def read_stacks(inputs):
stacks = {}
for line in inputs:
for idx, crate in enumerate(chunk(line, 4)):
if not ''.join(crate).strip():
continue
if crate[1] in string.digits:
for key in stacks.keys():
stacks[key].reverse()
return stacks
stack = idx+1
# print(''.join(crate[0:]), stack)
if stack not in stacks:
stacks[stack] = []
stacks[stack].append(crate[1])
# print(stacks)
def process_move_single(move, stacks):
_, num, _, from_, _, to = move.split()
for crate in range(0, int(num)):
stacks[int(to)].append(stacks[int(from_)].pop())
def process_move_multiple(move, stacks):
_, num, _, from_, _, to = move.split()
num = int(num)
from_ = int(from_)
to = int(to)
stacks[to].extend(stacks[from_][-num:])
# print(from_, stacks[from_][-num:], to)
del stacks[from_][-num:]
with open('input') as f:
file = [line.rstrip('\n') for line in f]
inputs = (line for line in file)
stacks = read_stacks(inputs)
# print(stacks)
for move in inputs:
if move.strip():
process_move_single(move, stacks)
# print(stacks)
top_crates = [stacks[crate][-1]
for crate in range(1, 9+1) if stacks[crate]]
# print(top_crates)
print(''.join(top_crates))
inputs = (line for line in file)
stacks = read_stacks(inputs)
# print(stacks)
for move in inputs:
if move.strip():
process_move_multiple(move, stacks)
# print(stacks)
top_crates = [stacks[crate][-1]
for crate in range(1, 9+1) if stacks[crate]]
# print(top_crates)
print(''.join(top_crates))