-
Notifications
You must be signed in to change notification settings - Fork 1
/
09.py
39 lines (35 loc) · 1.1 KB
/
09.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
"""--- Day 9: All in a Single Night ---"""
def recurse(current, routes, distance):
if len(routes) > 1:
for route in routes:
# python lists are a bastard
r = list(routes)
# been there, done that
r.remove(route)
# next stop ...
recurse(route, r, distance + destinations[current][route])
else:
# final destination
final_destinations.append(distance + destinations[current][routes[0]])
def day_9(instructions):
# build destinations
for line in instructions:
location, _, destination, _, distance = line.split(" ")
if location not in destinations:
destinations[location] = {}
if destination not in destinations:
destinations[destination] = {}
# double-pack
destinations[location][destination] = destinations[destination][location] = int(distance)
# bootstrap
for route, _ in destinations.items():
r = list(destinations.keys())
r.remove(route)
recurse(route, r, 0)
return final_destinations
with open("input/09.txt") as f:
final_destinations = []
destinations = {}
res = day_9(f)
print(min(res))
print(max(res))