-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.py
executable file
·67 lines (45 loc) · 1.48 KB
/
day06.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
#!/usr/bin/env python3
import aoc
def parse_input(lines):
map_data = {}
for line in lines:
(center, outside) = line.split(')')
map_data[outside] = center
return map_data
def get_path_to_com(map_data, orbiting_object):
path = []
while orbiting_object != "COM":
orbiting_object = map_data[orbiting_object]
path.append(orbiting_object)
return path
def count_orbits(map_data):
count = 0
for orbiting_object in map_data:
path = get_path_to_com(map_data, orbiting_object)
count += len(path)
return count
def get_orbits_apart(map_data, object1, object2):
path1 = get_path_to_com(map_data, object1)
path2 = get_path_to_com(map_data, object2)
path1.reverse()
path2.reverse()
# Calculate the length of common part of the path to COM.
count = 0
for (first, second) in zip(path1, path2):
if first == second:
count += 1
else:
break
# The number of orbits apart is the length of the paths that is not common to both.
orbits_apart = len(path1) - count + len(path2) - count
return orbits_apart
def part1(input_list):
map_data = parse_input(input_list)
number_of_orbits = count_orbits(map_data)
return number_of_orbits
def part2(input_list):
map_data = parse_input(input_list)
number_of_transfers = get_orbits_apart(map_data, "SAN", "YOU")
return number_of_transfers
if __name__ == "__main__":
aoc.main(part1, part2)