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}") | ||||
							
								
								
									
										28
									
								
								src/holt59/aoc/inputs/holt59/2015/day9.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/holt59/aoc/inputs/holt59/2015/day9.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
							
								
								
									
										3
									
								
								src/holt59/aoc/inputs/tests/2015/day9.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/holt59/aoc/inputs/tests/2015/day9.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| London to Dublin = 464 | ||||
| London to Belfast = 518 | ||||
| Dublin to Belfast = 141 | ||||
		Reference in New Issue
	
	Block a user