-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
36 lines (30 loc) · 1.04 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
import math
from GhostyUtils import aoc
if __name__ == '__main__':
directions, nodes = aoc.read_sections()
nodes = nodes.split('\n')
graph = {}
for node in nodes:
label, connections = node.split(' = ')
graph[label] = connections[1:-1].split(', ')
def navigate(count):
return 'LR'.index(directions[count % len(directions)])
node = 'AAA'
count = 0
while node != 'ZZZ':
if 'AAA' not in graph or 'ZZZ' not in graph:
break
node = graph[node][navigate(count)]
count += 1
print(count)
nodes = list(filter(lambda n: n.endswith('A'), graph))
paths = [{'start': node, 'period': 0} for node in nodes]
count = 0
while not all(path['period'] != 0 for path in paths):
for i, node in enumerate(nodes):
if node.endswith('Z'):
if paths[i]['period'] == 0:
paths[i]['period'] = count
nodes[i] = graph[node][navigate(count)]
count += 1
print(math.lcm(*(path['period'] for path in paths)))