first commit
This commit is contained in:
28
09/input
Normal file
28
09/input
Normal file
@ -0,0 +1,28 @@
|
||||
Faerun to Norrath = 129
|
||||
Faerun to Tristram = 58
|
||||
Faerun to AlphaCentauri = 13
|
||||
Faerun to Arbre = 24
|
||||
Faerun to Snowdin = 60
|
||||
Faerun to Tambi = 71
|
||||
Faerun to Straylight = 67
|
||||
Norrath to Tristram = 142
|
||||
Norrath to AlphaCentauri = 15
|
||||
Norrath to Arbre = 135
|
||||
Norrath to Snowdin = 75
|
||||
Norrath to Tambi = 82
|
||||
Norrath to Straylight = 54
|
||||
Tristram to AlphaCentauri = 118
|
||||
Tristram to Arbre = 122
|
||||
Tristram to Snowdin = 103
|
||||
Tristram to Tambi = 49
|
||||
Tristram to Straylight = 97
|
||||
AlphaCentauri to Arbre = 116
|
||||
AlphaCentauri to Snowdin = 12
|
||||
AlphaCentauri to Tambi = 18
|
||||
AlphaCentauri to Straylight = 91
|
||||
Arbre to Snowdin = 129
|
||||
Arbre to Tambi = 53
|
||||
Arbre to Straylight = 40
|
||||
Snowdin to Tambi = 15
|
||||
Snowdin to Straylight = 99
|
||||
Tambi to Straylight = 70
|
||||
3
09/input1
Normal file
3
09/input1
Normal file
@ -0,0 +1,3 @@
|
||||
London to Dublin = 464
|
||||
London to Belfast = 518
|
||||
Dublin to Belfast = 141
|
||||
61
09/part1.py
Normal file
61
09/part1.py
Normal file
@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
|
||||
RE_DISTANCE = re.compile(r'^(.*) to (.*) = (\d+)$')
|
||||
|
||||
|
||||
class Town:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.distance = dict()
|
||||
|
||||
def addDistance(self, town, distance):
|
||||
self.distance[town] = distance
|
||||
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
|
||||
def computePossiblePaths(town, neighbours, maxTowns, visited, journey):
|
||||
visited.append(town)
|
||||
if len(visited) == maxTowns:
|
||||
print "%010d %s" % (journey, ",".join(visited))
|
||||
visited.pop()
|
||||
return
|
||||
for nei in neighbours[town].distance.keys():
|
||||
if nei in visited:
|
||||
continue
|
||||
distance = neighbours[town].distance[nei]
|
||||
journey += distance
|
||||
computePossiblePaths(nei, neighbours, maxTowns, visited, journey)
|
||||
journey -= distance
|
||||
visited.pop()
|
||||
|
||||
|
||||
def main():
|
||||
towns = dict()
|
||||
for line in read_file('input'):
|
||||
match = re.search(RE_DISTANCE, line)
|
||||
if not match:
|
||||
print "wrong operation syntax"
|
||||
if match.group(1) not in towns:
|
||||
towns[match.group(1)] = Town(match.group(1))
|
||||
if match.group(2) not in towns:
|
||||
towns[match.group(2)] = Town(match.group(2))
|
||||
towns[match.group(1)].addDistance(match.group(2), int(match.group(3)))
|
||||
towns[match.group(2)].addDistance(match.group(1), int(match.group(3)))
|
||||
|
||||
for town in towns.keys():
|
||||
print "Starting point: %s" % (town)
|
||||
computePossiblePaths(town, towns, len(towns.keys()), [], 0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
61
09/part2.py
Normal file
61
09/part2.py
Normal file
@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
|
||||
RE_DISTANCE = re.compile(r'^(.*) to (.*) = (\d+)$')
|
||||
|
||||
|
||||
class Town:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.distance = dict()
|
||||
|
||||
def addDistance(self, town, distance):
|
||||
self.distance[town] = distance
|
||||
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
|
||||
def computePossiblePaths(town, neighbours, maxTowns, visited, journey):
|
||||
visited.append(town)
|
||||
if len(visited) == maxTowns:
|
||||
print "%010d %s" % (journey, ",".join(visited))
|
||||
visited.pop()
|
||||
return
|
||||
for nei in neighbours[town].distance.keys():
|
||||
if nei in visited:
|
||||
continue
|
||||
distance = neighbours[town].distance[nei]
|
||||
journey += distance
|
||||
computePossiblePaths(nei, neighbours, maxTowns, visited, journey)
|
||||
journey -= distance
|
||||
visited.pop()
|
||||
|
||||
|
||||
def main():
|
||||
towns = dict()
|
||||
for line in read_file('input'):
|
||||
match = re.search(RE_DISTANCE, line)
|
||||
if not match:
|
||||
print "wrong operation syntax"
|
||||
if match.group(1) not in towns:
|
||||
towns[match.group(1)] = Town(match.group(1))
|
||||
if match.group(2) not in towns:
|
||||
towns[match.group(2)] = Town(match.group(2))
|
||||
towns[match.group(1)].addDistance(match.group(2), int(match.group(3)))
|
||||
towns[match.group(2)].addDistance(match.group(1), int(match.group(3)))
|
||||
|
||||
for town in towns.keys():
|
||||
print "Starting point: %s" % (town)
|
||||
computePossiblePaths(town, towns, len(towns.keys()), [], 0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user