-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.py
204 lines (170 loc) · 6.79 KB
/
day08.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import aoc
import math
"""
--- Day 8: Haunted Wasteland ---
You're still riding a camel across Desert Island when you spot a sandstorm quickly approaching.
When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago.
One of the camel's pouches is labeled "maps" - sure enough, it's full of documents (your puzzle input) about how to navigate the desert.
At least, you're pretty sure that's what they are; one of the documents contains a list of left/right instructions, and the rest of the documents seem to describe some kind of network of labeled nodes.
It seems like you're meant to use the left/right instructions to navigate the network.
Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland!
After examining the maps for a bit, two nodes stick out: AAA and ZZZ.
You feel like AAA is where you are now, and you have to follow the left/right instructions until you reach ZZZ.
This format defines each node of the network individually.
For example:
RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)
Starting with AAA, you need to look up the next element based on the next left/right instruction in your input.
In this example, start with AAA and go right (R) by choosing the right element of AAA, CCC.
Then, L means to choose the left element of CCC, ZZZ.
By following the left/right instructions, you reach ZZZ in 2 steps.
Of course, you might not find ZZZ right away.
If you run out of left/right instructions, repeat the whole sequence of instructions as necessary: RL really means RLRLRLRLRLRLRLRL...and so on.
For example, here is a situation that takes 6 steps to reach ZZZ:
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
Starting at AAA, follow the left/right instructions.
How many steps are required to reach ZZZ?
Your puzzle answer was 12169.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
The sandstorm is upon you and you aren't any closer to escaping the wasteland.
You had the camel follow the instructions, but you've barely left your starting position.
It's going to take significantly more steps to escape!
What if the map isn't for people - what if the map is for ghosts?
Are ghosts even bound by the laws of spacetime?
Only one way to find out.
After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z!
If you were a ghost, you'd probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.
For example:
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)
Here, there are two starting nodes, 11A and 22A (because they both end with A).
As you follow each left/right instruction, use that instruction to simultaneously navigate away from both nodes you're currently on.
Repeat this process until all of the nodes you're currently on end with Z.
(If only some of the nodes you're on end with Z, they act like any other node and you continue as normal.) In this example, you would proceed as follows:
Step 0: You are at 11A and 22A.
Step 1: You choose all of the left paths, leading you to 11B and 22B.
Step 2: You choose all of the right paths, leading you to 11Z and 22C.
Step 3: You choose all of the left paths, leading you to 11B and 22Z.
Step 4: You choose all of the right paths, leading you to 11Z and 22B.
Step 5: You choose all of the left paths, leading you to 11B and 22C.
Step 6: You choose all of the right paths, leading you to 11Z and 22Z.
So, in this example, you end up entirely on nodes that end in Z after 6 steps.
Simultaneously start on every node that ends with A.
How many steps does it take before you're only on nodes that end with Z?
"""
def solvePart1(lines):
nodes = {}
instructions = []
headNode = None
for l in lines:
if l.strip() == "":
continue
if " = " not in l:
instructions = l.strip()
continue
toks = l.strip().split("=")
node = toks[0].strip()
if headNode == None:
headNode = node
rhs = toks[1].strip().split(",")
left = rhs[0].strip()[1:].strip()
right = rhs[1].strip()[:-1].strip()
nodes[node] = (left, right)
instrCount = len(instructions)
node = "AAA"
result = 0
while (node != "ZZZ"):
instrIndex = result % instrCount
instr = instructions[instrIndex]
if instr == 'L':
nextNode = nodes[node][0]
elif instr == 'R':
nextNode = nodes[node][1]
else:
exit("bad instr:"+instr)
node = nextNode
result += 1
return result
def solvePart2(lines):
nodes = {}
instructions = []
headNode = None
for l in lines:
if l.strip() == "":
continue
if " = " not in l:
instructions = l.strip()
continue
toks = l.strip().split("=")
node = toks[0].strip()
if headNode == None:
headNode = node
rhs = toks[1].strip().split(",")
left = rhs[0].strip()[1:].strip()
right = rhs[1].strip()[:-1].strip()
nodes[node] = (left, right)
instrCount = len(instructions)
activeNodes = []
cycles = []
for n in nodes:
if n.endswith('A'):
activeNodes.append(n)
cycles.append(0)
result = 0
node = "ZZZ"
cyclesStart = []
for n in nodes:
cyclesStart.append(result)
while True:
for i in range(len(activeNodes)):
n = activeNodes[i]
if n.endswith('Z'):
cycleLength = result - cyclesStart[i]
if cycleLength != cycles[i]:
cycles[i] = cycleLength
cyclesStart[i] = result + 1
lcm = 1
for c in cycles:
if c == 0:
lcm = 0
break
lcm = lcm * c // math.gcd(lcm,c)
if lcm > 0:
return lcm
instrIndex = result % instrCount
instr = instructions[instrIndex]
nextNodes = []
if instr == 'L':
for n in activeNodes:
nextNode = nodes[n][0]
nextNodes.append(nextNode)
elif instr == 'R':
for n in activeNodes:
nextNode = nodes[n][1]
nextNodes.append(nextNode)
else:
exit("bad instr:"+instr)
activeNodes = nextNodes
result += 1
return result
def main():
aoc.run_day(8, solvePart1, 12169, solvePart2, 12030780859469)
if __name__ == '__main__':
main()