forked from tomkooij/AdventOfCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
47 lines (33 loc) · 1.14 KB
/
day9.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# adventofcode
# day 9.1
# brute force exhaustive search of the travelling salesman problem (!?)
from collections import defaultdict
from itertools import permutations
INPUTFILE = 'input/input9'
TESTCASE = ['London to Dublin = 464','London to Belfast = 518','Dublin to Belfast = 141']
def distance(graph, route):
"""recusive computation of distance in graph via route"""
nextstep = graph[route[0]][route[1]] # distance between first and second city
if len(route) > 2:
return nextstep + distance(graph, route[1:])
else:
return nextstep
with open(INPUTFILE) as f:
lines = f.readlines()
graph = defaultdict(dict) # graph of distances between cities (nodes)
nodes = []
for line in lines:
_from, temp = line.split(' to ')
_to, dist = temp.split(' = ')
graph[_from][_to] = int(dist)
graph[_to][_from] = int(dist)
nodes.append(_from)
nodes.append(_to)
nodes = list(set(nodes))
solution = []
for route in permutations(nodes):
d = distance(graph, route)
#print route, d
solution.append(d)
print "min = ", min(solution)
print "max = ", max(solution)