-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
41 lines (32 loc) · 972 Bytes
/
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
37
38
39
40
41
from GhostyUtils import aoc
from collections import defaultdict
def bfs(graph, start, end):
visited = set()
frontier = [[start]]
while frontier:
path = frontier.pop(0)
node = path[-1]
if node == end:
return path
for new_node in graph[node]:
if new_node in visited:
continue
frontier.append(path + [new_node])
visited.add(new_node)
def main():
orbits = defaultdict(set)
satellites = set()
for orbit in aoc.read_lines():
body, satellite = orbit.split(')')
orbits[body].add(satellite)
orbits[satellite].add(body)
satellites.add(satellite)
total_orbits = 0
for satellite in satellites:
path = bfs(orbits, 'COM', satellite)
total_orbits += len(path) - 1
print('p1:', total_orbits)
path = bfs(orbits, 'YOU', 'SAN')
print('p2:', len(path)-2-1)
if __name__ == "__main__":
main()