advent-of-code/src/holt59/aoc/2015/day9.py
Mikael CAPELLE f373528b06 2015 day 9.
2024-01-05 14:46:05 +01:00

27 lines
756 B
Python

import itertools
import sys
from collections import defaultdict
from typing import cast
import parse # type: ignore
lines = sys.stdin.read().splitlines()
distances: dict[str, dict[str, int]] = defaultdict(dict)
for line in lines:
origin, destination, length = cast(
tuple[str, str, int], parse.parse("{} to {} = {:d}", line) # type: ignore
)
distances[origin][destination] = distances[destination][origin] = length
distance_of_routes = {
route: sum(distances[o][d] for o, d in zip(route[:-1], route[1:]))
for route in map(tuple, itertools.permutations(distances))
}
answer_1 = min(distance_of_routes.values())
print(f"answer 1 is {answer_1}")
answer_2 = max(distance_of_routes.values())
print(f"answer 2 is {answer_2}")