|
6 | 6 |
|
7 | 7 | def main(): |
8 | 8 | text_input = get_raw_input() |
9 | | - pass |
| 9 | + raw_orbits = text_input.splitlines() |
| 10 | + orbits = [x.split(')') for x in raw_orbits] |
| 11 | + |
| 12 | + space_objects = dict() |
10 | 13 |
|
| 14 | + # Add all different space objects |
| 15 | + for orbit in orbits: |
| 16 | + if orbit[0] not in space_objects.keys(): |
| 17 | + if orbit[0] == 'COM': |
| 18 | + space_objects['COM'] = Com() |
| 19 | + else: |
| 20 | + space_objects[orbit[0]] = SpaceObject(orbit[0]) |
| 21 | + if orbit[1] not in space_objects.keys(): |
| 22 | + space_objects[orbit[1]] = SpaceObject(orbit[1]) |
| 23 | + |
| 24 | + # Set parent for every space object |
| 25 | + for orbit in orbits: |
| 26 | + space_objects[orbit[1]].set_parent(space_objects[orbit[0]]) |
| 27 | + |
| 28 | + total_indirect_orbits = 0 |
| 29 | + # Find number of indirect orbits for every space objects, add to sum |
| 30 | + for id_ in space_objects.keys(): |
| 31 | + if isinstance(space_objects[id_], Com): |
| 32 | + continue |
| 33 | + total_indirect_orbits += space_objects[id_].get_parent_orbits_count() |
| 34 | + |
| 35 | + print(total_indirect_orbits) |
| 36 | + |
| 37 | + """ |
| 38 | + for key in space_objects.keys(): |
| 39 | + print(space_objects[key]) |
| 40 | + """ |
| 41 | + |
| 42 | + |
| 43 | +class SpaceObject: |
| 44 | + def __init__(self, id_): |
| 45 | + self.id = id_ |
| 46 | + self.parent = None |
| 47 | + self.children = [] |
| 48 | + |
| 49 | + def set_parent(self, parent): |
| 50 | + self.parent = parent |
| 51 | + self.parent.add_child(self) |
| 52 | + |
| 53 | + def add_child(self, child): |
| 54 | + self.children.append(child) |
| 55 | + |
| 56 | + def get_parent_orbits_count(self): |
| 57 | + return self.parent.get_parent_orbits_count() + 1 |
| 58 | + |
| 59 | + def __str__(self): |
| 60 | + return f'{repr(self)} ({repr(self.parent)}): {self.children}' |
| 61 | + |
| 62 | + def __repr__(self): |
| 63 | + return str(self.id) |
| 64 | + |
| 65 | + |
| 66 | +class Com(SpaceObject): |
| 67 | + def __init__(self): |
| 68 | + super().__init__('COM') |
| 69 | + |
| 70 | + def get_parent_orbits_count(self): |
| 71 | + return 0 |
| 72 | + |
| 73 | + def __str__(self): |
| 74 | + return f'COM: {self.children}' |
11 | 75 |
|
12 | 76 | def get_raw_input(): |
13 | 77 | return open(retrieve_input_file_path(), 'r').read() |
|
0 commit comments