From 04172beb5a8432fe1e3b87f0381aee724893c9bb Mon Sep 17 00:00:00 2001 From: Mikael CAPELLE Date: Fri, 5 Jan 2024 14:46:05 +0100 Subject: [PATCH] 2015 day 9. --- src/holt59/aoc/2015/day9.py | 26 ++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day9.txt | 28 ++++++++++++++++++++++ src/holt59/aoc/inputs/tests/2015/day9.txt | 3 +++ 3 files changed, 57 insertions(+) create mode 100644 src/holt59/aoc/2015/day9.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day9.txt create mode 100644 src/holt59/aoc/inputs/tests/2015/day9.txt diff --git a/src/holt59/aoc/2015/day9.py b/src/holt59/aoc/2015/day9.py new file mode 100644 index 0000000..df8e5e0 --- /dev/null +++ b/src/holt59/aoc/2015/day9.py @@ -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}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day9.txt b/src/holt59/aoc/inputs/holt59/2015/day9.txt new file mode 100644 index 0000000..97a6b63 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day9.txt @@ -0,0 +1,28 @@ +Faerun to Tristram = 65 +Faerun to Tambi = 129 +Faerun to Norrath = 144 +Faerun to Snowdin = 71 +Faerun to Straylight = 137 +Faerun to AlphaCentauri = 3 +Faerun to Arbre = 149 +Tristram to Tambi = 63 +Tristram to Norrath = 4 +Tristram to Snowdin = 105 +Tristram to Straylight = 125 +Tristram to AlphaCentauri = 55 +Tristram to Arbre = 14 +Tambi to Norrath = 68 +Tambi to Snowdin = 52 +Tambi to Straylight = 65 +Tambi to AlphaCentauri = 22 +Tambi to Arbre = 143 +Norrath to Snowdin = 8 +Norrath to Straylight = 23 +Norrath to AlphaCentauri = 136 +Norrath to Arbre = 115 +Snowdin to Straylight = 101 +Snowdin to AlphaCentauri = 84 +Snowdin to Arbre = 96 +Straylight to AlphaCentauri = 107 +Straylight to Arbre = 14 +AlphaCentauri to Arbre = 46 diff --git a/src/holt59/aoc/inputs/tests/2015/day9.txt b/src/holt59/aoc/inputs/tests/2015/day9.txt new file mode 100644 index 0000000..2bceede --- /dev/null +++ b/src/holt59/aoc/inputs/tests/2015/day9.txt @@ -0,0 +1,3 @@ +London to Dublin = 464 +London to Belfast = 518 +Dublin to Belfast = 141