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}")