summary refs log tree commit diff stats
path: root/day9.py
blob: 4ce3f69391b33d4a5b99d63cb3c034e7370a12ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)