summary refs log tree commit diff stats
path: root/day9.py
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-30 15:11:21 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-30 15:11:21 -0800
commite7085453864431ace3ad8f3123b259ed0829ae74 (patch)
tree2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day9.py
downloadAdventOfCode2015-e7085453864431ace3ad8f3123b259ed0829ae74.tar.gz
all solutions for 2015 main
Diffstat (limited to 'day9.py')
-rw-r--r--day9.py29
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)