mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Solutions for day 9.
This commit is contained in:
28
day9-input.txt
Normal file
28
day9-input.txt
Normal file
@@ -0,0 +1,28 @@
|
||||
Tristram to AlphaCentauri = 34
|
||||
Tristram to Snowdin = 100
|
||||
Tristram to Tambi = 63
|
||||
Tristram to Faerun = 108
|
||||
Tristram to Norrath = 111
|
||||
Tristram to Straylight = 89
|
||||
Tristram to Arbre = 132
|
||||
AlphaCentauri to Snowdin = 4
|
||||
AlphaCentauri to Tambi = 79
|
||||
AlphaCentauri to Faerun = 44
|
||||
AlphaCentauri to Norrath = 147
|
||||
AlphaCentauri to Straylight = 133
|
||||
AlphaCentauri to Arbre = 74
|
||||
Snowdin to Tambi = 105
|
||||
Snowdin to Faerun = 95
|
||||
Snowdin to Norrath = 48
|
||||
Snowdin to Straylight = 88
|
||||
Snowdin to Arbre = 7
|
||||
Tambi to Faerun = 68
|
||||
Tambi to Norrath = 134
|
||||
Tambi to Straylight = 107
|
||||
Tambi to Arbre = 40
|
||||
Faerun to Norrath = 11
|
||||
Faerun to Straylight = 66
|
||||
Faerun to Arbre = 144
|
||||
Norrath to Straylight = 115
|
||||
Norrath to Arbre = 135
|
||||
Straylight to Arbre = 127
|
||||
63
day9.py
Normal file
63
day9.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import re
|
||||
import fileinput
|
||||
|
||||
dist = {}
|
||||
visited = set()
|
||||
|
||||
def computeMinDistance(startPoint):
|
||||
visited.add(startPoint)
|
||||
|
||||
if len(dist) == len(visited):
|
||||
maximum = 0
|
||||
minimum = 0
|
||||
else:
|
||||
minimum = None
|
||||
maximum = 0
|
||||
|
||||
for i in dist[startPoint]:
|
||||
if i in visited:
|
||||
continue
|
||||
|
||||
shortest, longest = computeMinDistance(i)
|
||||
|
||||
if shortest is not None:
|
||||
shortest += dist[startPoint][i]
|
||||
longest += dist[startPoint][i]
|
||||
|
||||
if minimum is None:
|
||||
minimum = shortest
|
||||
maximum = longest
|
||||
else:
|
||||
minimum = min(minimum, shortest)
|
||||
maximum = max(maximum, longest)
|
||||
|
||||
visited.remove(startPoint)
|
||||
|
||||
return minimum, maximum
|
||||
|
||||
|
||||
|
||||
linePattern = re.compile(r"(\w+) to (\w+) = (\d+)")
|
||||
|
||||
for line in fileinput.input():
|
||||
match = linePattern.match(line)
|
||||
if match.group(1) not in dist:
|
||||
dist[match.group(1)] = {}
|
||||
|
||||
if match.group(2) not in dist:
|
||||
dist[match.group(2)] = {}
|
||||
|
||||
dist[match.group(1)][match.group(2)] = int(match.group(3))
|
||||
dist[match.group(2)][match.group(1)] = int(match.group(3))
|
||||
|
||||
minimum = None
|
||||
possible = []
|
||||
for i in dist:
|
||||
shortest, longest = computeMinDistance(i)
|
||||
|
||||
if shortest is not None:
|
||||
possible.append(shortest)
|
||||
possible.append(longest)
|
||||
|
||||
print "Shortest path is", min(possible)
|
||||
print "Longest path is", max(possible)
|
||||
Reference in New Issue
Block a user