mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-12-22 05:41:51 +01:00
Implement day 6 part 1
This commit is contained in:
parent
476fb7e0ee
commit
6a69ee4255
3 changed files with 1163 additions and 0 deletions
42
2019/day06-orbits.py
Normal file
42
2019/day06-orbits.py
Normal 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
1110
2019/inputs/day06
Normal file
File diff suppressed because it is too large
Load diff
11
2019/inputs/day06-ex1
Normal file
11
2019/inputs/day06-ex1
Normal 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
|
Loading…
Reference in a new issue