My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 33 lines 866 B view raw
1#!/usr/bin/env python 2import fileinput 3import itertools 4import re 5 6def parse_line(line): 7 departure, arrival, distance = re.match(r'(\w+) to (\w+) = (\d+)', line).groups() 8 return departure, arrival, int(distance) 9 10def sum_of_paths(d): 11 # Yield sum of all possible routes 12 for p in itertools.permutations(locations, len(locations)): 13 yield sum(d[p[i]][p[i+1]] for i in range(len(p) - 1)) 14 15distances = {} 16 17# Parse input 18for line in fileinput.input(): 19 frm, to, dist = parse_line(line) 20 if distances.get(frm): 21 distances[frm][to] = dist 22 else: 23 distances[frm] = {to: dist} 24 25 if distances.get(to): 26 distances[to][frm] = dist 27 else: 28 distances[to] = {frm: dist} 29 30locations = distances.keys() 31 32print "Shortest route: %d" % min(sum_of_paths(distances)) 33print "Longest route: %d" % max(sum_of_paths(distances))