-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.py
73 lines (60 loc) · 2.03 KB
/
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
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
# --- Day 5: Supply Stacks ---
# I have to move crates between stacks
# Manually initialised stacks (also removed this data from the input file)
stacks = [['J', 'H', 'G', 'M', 'Z', 'N', 'T', 'F'],
['V', 'W', 'J'],
['G', 'V', 'L', 'J', 'B', 'T', 'H'],
['B', 'P', 'J', 'N', 'C', 'D', 'V', 'L'],
['F', 'W', 'S', 'M', 'P', 'R', 'G'],
['G', 'H', 'C', 'F', 'B', 'N', 'V', 'M'],
['D', 'H', 'G', 'M', 'R'],
['H', 'N', 'M', 'V', 'Z', 'D'],
['G', 'N', 'F', 'H']]
# Open file
f = open("5_input.txt", "r")
# Move crates between stacks
for line in f:
# Parse
split_line = line.split()
quantity = int(split_line[1])
start_stack = int(split_line[3]) - 1
end_stack = int(split_line[5]) - 1
# Move
for _ in range(quantity):
temp = stacks[start_stack].pop()
stacks[end_stack].append(temp)
top_crates = ""
for s in stacks:
top_crates += s[-1]
print("Part 1 top crates:", top_crates)
# --- Part Two ---
# Crates now retain their order when moved
# Manually initialised stacks
stacks = [['J', 'H', 'G', 'M', 'Z', 'N', 'T', 'F'],
['V', 'W', 'J'],
['G', 'V', 'L', 'J', 'B', 'T', 'H'],
['B', 'P', 'J', 'N', 'C', 'D', 'V', 'L'],
['F', 'W', 'S', 'M', 'P', 'R', 'G'],
['G', 'H', 'C', 'F', 'B', 'N', 'V', 'M'],
['D', 'H', 'G', 'M', 'R'],
['H', 'N', 'M', 'V', 'Z', 'D'],
['G', 'N', 'F', 'H']]
# Open file
f = open("5_input.txt", "r")
# Move crates between stacks
for line in f:
# Parse
split_line = line.split()
quantity = int(split_line[1])
start_stack = int(split_line[3]) - 1
end_stack = int(split_line[5]) - 1
# Move
temp = []
for _ in range(quantity):
temp.append(stacks[start_stack].pop())
for _ in range(len(temp)):
stacks[end_stack].append(temp.pop())
top_crates = ""
for s in stacks:
top_crates += s[-1]
print("Part 2 top crates:", top_crates)