aoc2019/day06.py
2021-12-27 17:31:33 +01:00

54 lines
1.1 KiB
Python

from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(42, "test_input06"),
(261306, "input06")
],
[
(4, "test_input06_2"),
(382, "input06")
]
]
def part1(self) -> Any:
tree = {}
for l in self.getInputAsArraySplit(')'):
tree[l[1]] = l[0]
count = 0
for child, parent in tree.items():
count += 1
while parent != "COM":
parent = tree[parent]
count += 1
return count
def part2(self) -> Any:
tree = {}
for l in self.getInputAsArraySplit(')'):
tree[l[1]] = l[0]
source = tree["YOU"]
target = tree["SAN"]
path = []
while source != "COM":
path.append(source)
source = tree[source]
count = 0
while target not in path:
count += 1
target = tree[target]
return count + path.index(target)
if __name__ == '__main__':
day = Day(2019, 6)
day.run(verbose=True)