Created
December 20, 2015 23:52
-
-
Save lwiecek/1aa0d37ea5f1ab560084 to your computer and use it in GitHub Desktop.
day9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from collections import defaultdict | |
dist = defaultdict(lambda: float('inf')) | |
cities = set() | |
with open('input9.txt') as f: | |
for line in f: | |
frags = line.strip().split() | |
src_dest = frags[0], frags[2] | |
dist[frozenset(src_dest)] = int(frags[4]) | |
cities.update(src_dest) | |
def distances(): | |
for perm in itertools.permutations(cities): | |
yield sum(dist[frozenset(pair)] for pair in zip(perm, perm[1:])) | |
distances = list(distances()) | |
print(min(distances)) # part one | |
print(max(distances)) # part two |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment