2015 day 9.
This commit is contained in:
26
src/holt59/aoc/2015/day9.py
Normal file
26
src/holt59/aoc/2015/day9.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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}")
|
Reference in New Issue
Block a user