Solved 2015/09 P1+P2 and added TSP (short/long) function and dijkstras to helper file

This commit is contained in:
2024-12-07 22:47:33 +01:00
parent 80a181656e
commit 1bf03abc75
5 changed files with 274 additions and 4 deletions
+58
View File
@@ -0,0 +1,58 @@
## \-\-- Day 9: All in a Single Night \-\--
Every year, Santa manages to deliver all of his presents in a single
night.
This year, however, he has some [new
locations]{title="Bonus points if you recognize all of the locations."}
to visit; his elves have provided him the distances between every pair
of locations. He can start and end at any two (different) locations he
wants, but he must visit each location exactly once. What is the
*shortest distance* he can travel to achieve this?
For example, given the following distances:
London to Dublin = 464
London to Belfast = 518
Dublin to Belfast = 141
The possible routes are therefore:
Dublin -> London -> Belfast = 982
London -> Dublin -> Belfast = 605
London -> Belfast -> Dublin = 659
Dublin -> Belfast -> London = 659
Belfast -> Dublin -> London = 605
Belfast -> London -> Dublin = 982
The shortest of these is `London -> Dublin -> Belfast = 605`, and so the
answer is `605` in this example.
What is the distance of the shortest route?
Your puzzle answer was `117`.
## \-\-- Part Two \-\-- {#part2}
The next year, just to show off, Santa decides to take the route with
the *longest distance* instead.
He can still start and end at any two (different) locations he wants,
and he still must visit each location exactly once.
For example, given the distances above, the longest route would be `982`
via (for example) `Dublin -> London -> Belfast`.
What is the distance of the longest route?
Your puzzle answer was `909`.
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2015) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](9/input).
+74
View File
@@ -0,0 +1,74 @@
#!/bin/python3
import sys,time,re,json
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile,dprint,TSP
start_time = time.time()
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def constructGraph(input_f):
"""
The graph of cities should look something like this
graph = {
"node": [("dist1", distance1), ("dist", distance2)],
}
"""
inst = []
graph = {}
with open(input_f) as file:
for line in file:
inst = line.rstrip().split(' = ')
inst = inst[0].split(' to ') + [int(inst[1])]
if inst[0] not in graph:
graph[inst[0]] = []
if inst[1] not in graph:
graph[inst[1]] = []
graph[inst[0]].append((inst[1],inst[2]))
graph[inst[1]].append((inst[0],inst[2]))
return graph
if part == 1:
graph = constructGraph(input_f)
shortest = float('inf')
routes = []
cities = set(graph.keys())
for neighbors in graph.values():
for neighbor, _ in neighbors:
cities.add(neighbor)
for c in cities:
print(TSP(graph, c))
shortest = min(shortest,TSP(graph, c)[1])
print(shortest)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
graph = constructGraph(input_f)
#print(graph)
longest = 0
routes = []
cities = set(graph.keys())
for neighbors in graph.values():
for neighbor, _ in neighbors:
cities.add(neighbor)
for c in cities:
longest = max(longest,TSP(graph, c,'longest')[1])
print(longest)
print("--- %s seconds ---" % (time.time() - start_time))