Skip to content

Commit 17230cd

Browse files
committed
added 2015/day09
1 parent 49be37b commit 17230cd

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

2015/day09/answers.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
117
2+
909

2015/day09/input.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Faerun to Tristram = 65
2+
Faerun to Tambi = 129
3+
Faerun to Norrath = 144
4+
Faerun to Snowdin = 71
5+
Faerun to Straylight = 137
6+
Faerun to AlphaCentauri = 3
7+
Faerun to Arbre = 149
8+
Tristram to Tambi = 63
9+
Tristram to Norrath = 4
10+
Tristram to Snowdin = 105
11+
Tristram to Straylight = 125
12+
Tristram to AlphaCentauri = 55
13+
Tristram to Arbre = 14
14+
Tambi to Norrath = 68
15+
Tambi to Snowdin = 52
16+
Tambi to Straylight = 65
17+
Tambi to AlphaCentauri = 22
18+
Tambi to Arbre = 143
19+
Norrath to Snowdin = 8
20+
Norrath to Straylight = 23
21+
Norrath to AlphaCentauri = 136
22+
Norrath to Arbre = 115
23+
Snowdin to Straylight = 101
24+
Snowdin to AlphaCentauri = 84
25+
Snowdin to Arbre = 96
26+
Straylight to AlphaCentauri = 107
27+
Straylight to Arbre = 14
28+
AlphaCentauri to Arbre = 46

2015/day09/run.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#! /usr/bin/env python3
2+
3+
def load_data(filename):
4+
with open(filename, 'r') as f:
5+
for line in f:
6+
line = line.rstrip('\n')
7+
t1 = line.split(' = ')
8+
t2 = t1[0].split(' to ')
9+
yield int(t1[1]), t2[0], t2[1]
10+
11+
# Part One
12+
13+
import itertools
14+
import networkx as nx
15+
16+
G = nx.Graph()
17+
18+
for distance, f, t in load_data('input.txt'):
19+
G.add_edge(f, t, weight=distance)
20+
21+
def solve_shortest_hamiltonian_path(G):
22+
nodes = list(G.nodes)
23+
num_nodes = len(nodes)
24+
25+
# Brute-force search over all permutations of nodes
26+
best_path = None
27+
min_cost = float("inf")
28+
29+
for perm in itertools.permutations(nodes): # Try all orderings
30+
try:
31+
cost = sum(G[perm[i]][perm[i+1]]['weight'] for i in range(num_nodes - 1))
32+
if cost < min_cost:
33+
min_cost = cost
34+
best_path = perm # This is the optimal open path
35+
except KeyError:
36+
# This catches cases where a path doesn't exist in non-complete graphs
37+
continue
38+
39+
return best_path, min_cost
40+
41+
path, cost = solve_shortest_hamiltonian_path(G)
42+
43+
print(cost)
44+
45+
# Part Two
46+
47+
def solve_longest_hamiltonian_path(G):
48+
nodes = list(G.nodes)
49+
num_nodes = len(nodes)
50+
51+
# Brute-force search over all permutations of nodes
52+
best_path = None
53+
max_cost = float("-inf") # Start with negative infinity
54+
55+
for perm in itertools.permutations(nodes): # Try all orderings
56+
try:
57+
cost = sum(G[perm[i]][perm[i+1]]['weight'] for i in range(num_nodes - 1))
58+
if cost > max_cost: # Maximization instead of minimization
59+
max_cost = cost
60+
best_path = perm # Store the best (longest) path
61+
except KeyError:
62+
# This catches cases where a path doesn't exist in non-complete graphs
63+
continue
64+
65+
return best_path, max_cost
66+
67+
path, cost = solve_longest_hamiltonian_path(G)
68+
69+
print(cost)
70+

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
```
22
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
3-
2015 ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3+
2015 ++ ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
44
2016 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
55
2017 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
66
2018 ++ ++ ++ ++ ++ ++ ++ ++ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +

0 commit comments

Comments
 (0)