-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5.py
More file actions
38 lines (30 loc) · 906 Bytes
/
day5.py
File metadata and controls
38 lines (30 loc) · 906 Bytes
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
crates, moves = map(
str.splitlines, open("../inputs/day5.txt").read().rstrip().split("\n\n")
)
def getStack(crates):
rows = crates[:-1]
stack = [[] for _ in range(9)]
for row in reversed(rows):
i = 0
for c in row:
if c.isupper():
stack[i // 4].append(c)
i += 1
return stack
def part1():
stack = getStack(crates)
for move in moves:
n, f, t = map(int, move.split(" ")[1::2])
for _ in range(n):
stack[t - 1].append(stack[f - 1].pop())
return "".join([t.pop() for t in stack])
def part2():
stack = getStack(crates)
for move in moves:
n, f, t = map(int, move.split(" ")[1::2])
stack[t - 1] += stack[f - 1][-n:]
stack[f - 1] = stack[f - 1][:-n]
return "".join([t.pop() for t in stack])
if __name__ == "__main__":
print(part1())
print(part2())