54 lines
1.9 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: MIT
# Copyright (c) 2019 Akumatic
#
# https://adventofcode.com/2019/day/6
2019-12-06 13:13:38 +01:00
def readFile() -> dict:
2019-12-06 13:13:38 +01:00
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
2019-12-06 17:32:20 +01:00
lines = [data[:-1].split(")") for data in f.readlines()]
return {line[1]: line[0] for line in lines}
2019-12-06 17:32:20 +01:00
def countOrbits(data: dict, cache: dict, node: str) -> int:
2019-12-06 17:32:20 +01:00
if node in cache: return cache[node]
cache[node] = 0 if node not in data else 1 + countOrbits(data,cache,data[node])
return cache[node]
def getIntersection(data: dict, cache: dict, node1: str, node2: str) -> str:
2019-12-06 17:32:20 +01:00
if cache[node1] > cache[node2]: node1, node2 = node2, node1
parents = set()
# get elements of shorter path
while data[node1] in data:
parents.add(data[node1])
node1 = data[node1]
parents.add(data[node1])
# look for first node present in both paths
while node2 not in parents:
node2 = data[node2]
return node2
def part1(vals : dict, cache) -> int:
2019-12-06 17:32:20 +01:00
return sum([countOrbits(vals,cache,val) for val in vals])
def part2(vals : dict, cache) -> int:
2019-12-06 17:32:20 +01:00
intersection = getIntersection(vals,cache,"YOU","SAN")
return cache["YOU"] + cache["SAN"] - 2*cache[intersection] - 2
2019-12-06 13:13:38 +01:00
def test():
2019-12-06 17:32:20 +01:00
vals, cache = {"B":"COM","C":"B","D":"C","E":"D","F":"E","G":"B",
"H":"G","I":"D","J":"E","K":"J","L":"K"}, {}
assert countOrbits(vals,cache,"D") == 3
assert countOrbits(vals,cache,"L") == 7
assert not countOrbits(vals,cache,"COM")
assert sum([countOrbits(vals,cache,val) for val in vals]) == 42
2019-12-06 13:13:38 +01:00
vals["YOU"] = "K"
vals["SAN"] = "I"
2019-12-06 17:32:20 +01:00
countOrbits(vals,cache,"YOU"), countOrbits(vals,cache,"SAN")
assert getIntersection(vals,cache,"YOU","SAN") == "D"
assert countOrbits(vals,cache,"YOU") + countOrbits(vals,cache,"SAN") - \
2*cache["D"] == 6
2019-12-06 13:13:38 +01:00
if __name__ == "__main__":
test()
2019-12-06 17:32:20 +01:00
vals, cache = readFile(), {}
print(f"Part 1: {part1(vals, cache)}")
print(f"Part 2: {part2(vals, cache)}")