Implement day 6 part 1

This commit is contained in:
Gabriel Augendre 2020-03-05 21:31:57 +01:00
parent 476fb7e0ee
commit 6a69ee4255
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
3 changed files with 1163 additions and 0 deletions

42
2019/day06-orbits.py Normal file
View file

@ -0,0 +1,42 @@
from dataclasses import dataclass
from typing import Dict
@dataclass
class Node:
name: str
orbits_over: "Node" = None
def is_origin(self) -> bool:
return self.name.lower() == "com"
def count_hops(self):
counter = 0
current = self
while not current.is_origin():
current = current.orbits_over
counter += 1
return counter
def main():
with open("inputs/day06") as f:
orbits = f.read().split()
objects = dict() # type: Dict[str, Node]
for orbit in orbits:
stator, rotor = orbit.split(")")
stator = objects.get(stator, Node(name=stator))
rotor = objects.get(rotor, Node(name=rotor))
rotor.orbits_over = stator
objects[stator.name] = stator
objects[rotor.name] = rotor
counter = 0
for obj in objects.values():
counter += obj.count_hops()
print(counter)
if __name__ == "__main__":
main()

1110
2019/inputs/day06 Normal file

File diff suppressed because it is too large Load diff

11
2019/inputs/day06-ex1 Normal file
View file

@ -0,0 +1,11 @@
B)C
COM)B
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L