-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06_solve_puzzle2.py
More file actions
55 lines (44 loc) · 886 Bytes
/
day06_solve_puzzle2.py
File metadata and controls
55 lines (44 loc) · 886 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from collections import defaultdict
def parse_map(_map):
""" Returns a dictionary where from you can look
up which center a given orbiter has """
orbits = {}
for orbit in _map.split("\n"):
center, orbiter = orbit.split(")")
orbits[orbiter] = center
return orbits
def count_orbits(_map, start_obj):
orbits = parse_map(_map)
obj = start_obj
path = {}
i = 0
while obj != "COM":
obj = orbits[obj]
i += 1
path[obj] = i
return path
def find_shortest_connection(_map):
you = count_orbits(_map, "YOU")
santa = count_orbits(_map, "SAN")
for center in santa.keys():
if you.get(center):
return santa[center] + you[center] - 2
test_map = """
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN
""".strip("\n \t")
assert find_shortest_connection(test_map) == 4
with open("input06.txt", "r") as f:
_map = f.read()
print(find_shortest_connection(_map))