-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06a.py
48 lines (33 loc) · 1.1 KB
/
day06a.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
class SpaceObject:
def __init__(self, name):
self.name = name
self.orbits = None
self.orbited_by = []
self.depth = 0
space_objects = {'COM': SpaceObject('COM')}
def main():
with open('day06_input.txt') as file:
for line in file:
process_orbit(line.strip())
# traverse starting from the COM object
orbit_count = 0
traversal_list = [space_objects['COM']]
while traversal_list:
obj = traversal_list.pop()
curr_depth = obj.depth
orbit_count += curr_depth
for so in obj.orbited_by:
so.depth = curr_depth + 1
traversal_list += obj.orbited_by
print(orbit_count)
def process_orbit(orbit):
orbited_name = orbit[:3]
orbiting_name = orbit[4:]
# Lookup or add from dictionary
orbited = space_objects.setdefault(orbited_name, SpaceObject(orbited_name))
orbiting = space_objects.setdefault(orbiting_name, SpaceObject(orbiting_name))
# Update the relationships
orbited.orbited_by.append(orbiting)
orbiting.orbits = orbited
if __name__ == '__main__':
main()