41 lines
910 B
Python
41 lines
910 B
Python
from aoc import AOCDay
|
|
from typing import Any
|
|
|
|
|
|
class Day(AOCDay):
|
|
test_solutions_p1 = [42]
|
|
test_solutions_p2 = [4]
|
|
|
|
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)
|