-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday05.py
executable file
·44 lines (31 loc) · 856 Bytes
/
day05.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
#!/usr/bin/env python3
import sys
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
raw = []
stacks = [None]
moves = []
for line in fin:
if line == '\n':
break
raw.append(line)
for i, column in enumerate(zip(*raw)):
if i % 4 == 1:
stacks.append(''.join(column[:-1]).lstrip())
for line in fin:
line = line.split()
moves.append((int(line[1]), int(line[3]), int(line[5])))
original = stacks[:]
for n, i, j in moves:
chunk = stacks[i][:n][::-1]
stacks[i] = stacks[i][n:]
stacks[j] = chunk + stacks[j]
top = ''.join(s[0] for s in stacks[1:])
print('Part 1:', top)
stacks = original
for n, i, j in moves:
chunk = stacks[i][:n]
stacks[i] = stacks[i][n:]
stacks[j] = chunk + stacks[j]
top = ''.join(s[0] for s in stacks[1:])
print('Part 2:', top)