diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
commit | e7085453864431ace3ad8f3123b259ed0829ae74 (patch) | |
tree | 2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day9.py | |
download | AdventOfCode2015-e7085453864431ace3ad8f3123b259ed0829ae74.tar.gz |
all solutions for 2015 main
Diffstat (limited to 'day9.py')
-rw-r--r-- | day9.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/day9.py b/day9.py new file mode 100644 index 0000000..4ce3f69 --- /dev/null +++ b/day9.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +from collections import defaultdict +from itertools import permutations + +graph = defaultdict(lambda: defaultdict(int)) +with open('day9.txt') as data: + for line in data: + pos1, _, pos2, _, dist = line.strip().split() + graph[pos1][pos2] = int(dist) + graph[pos2][pos1] = int(dist) + +routes = list(permutations(graph.keys())) + +def route_dist(route): + dist = 0 + for i in range(len(route)-1): + dist += graph[route[i]][route[i+1]] + return dist + +minDist, maxDist = float('inf'), 0 +for route in routes: + dist = route_dist(route) + if dist < minDist: + minDist = dist + if dist > maxDist: + maxDist = dist +print(minDist) +print(maxDist) |