-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_06a.py
46 lines (38 loc) · 1.19 KB
/
day_06a.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
#!/usr/bin/python3
import sys
def recursiveOrbitCount(current_object, is_orbitted_by, n_orbits):
if current_object not in is_orbitted_by.keys():
n_orbits[current_object] = 0
else:
n_orbits[current_object] = 0
for orbitting_object in is_orbitted_by[current_object]:
recursiveOrbitCount(orbitting_object, is_orbitted_by, n_orbits)
n_orbits[current_object] += n_orbits[orbitting_object] + 1
def main():
# Get input
file_name = "../input/day_06_input"
if len(sys.argv) > 1:
file_name = sys.argv[1]
file = open(file_name)
orbits = dict()
is_orbitted_by = dict()
n_orbits = dict()
input = list()
for line in file:
line = line.strip("\n")
input.append(line)
# Solve
for line in input:
orbit = line.split(")")
orbits[orbit[1]] = orbit[0]
if orbit[0] not in is_orbitted_by.keys():
is_orbitted_by[orbit[0]] = set()
is_orbitted_by[orbit[0]].add(orbit[1])
recursiveOrbitCount("COM", is_orbitted_by, n_orbits)
total = 0
for n in n_orbits.values():
total += n
print(total)
return total
if __name__ == "__main__":
main()