-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.py
More file actions
51 lines (38 loc) · 1.26 KB
/
09.py
File metadata and controls
51 lines (38 loc) · 1.26 KB
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
48
49
routes = [x.strip() for x in open('09.input', 'r').readlines()]
paths = {}
for route in routes:
src, to, dest, eq, dist = route.split(' ')
if src not in paths:
paths[src] = []
if dest not in paths:
paths[dest] = []
paths[src].append((dest, dist))
paths[dest].append((src, dist))
def create_paths(path):
last = path[-1]
if last[0] not in paths:
return [path]
new_paths = [path]
nxt = paths[last[0]]
for n in nxt:
if n[0] not in [pp[0] for pp in path]:
new_p = create_paths(path + [n])
new_paths += new_p
return new_paths
nbr_cities = len(paths.keys())
shortest = 9999
longest = 0
for path in paths:
x = [p for p in create_paths([(path, 0)]) if len(p) == nbr_cities ]
for p in x:
# print(" -> ".join([route[0] for route in p]))
# print(" -> ".join([str(route[1]) for route in p]))
distances = [route[1] for route in p]
distance = sum([int(d) for d in distances])
# print("Dist %d" % distance)
shortest = shortest if shortest < distance else distance
longest = longest if longest > distance else distance
print("Part 1 solution: %d" % shortest)
print("Part 2 solution: %d" % longest)
# high: 474
# high: 719