Browse Source

Day 9

master
Chris Smith 8 years ago
parent
commit
a277908474
2 changed files with 63 additions and 0 deletions
  1. 35
    0
      9.py
  2. 28
    0
      9.txt

+ 35
- 0
9.py View File

@@ -0,0 +1,35 @@
1
+#!/usr/bin/python
2
+
3
+import re
4
+
5
+with open('9.txt', 'r') as file:
6
+    input = file.read()
7
+
8
+FUNC = max  # min for part 1
9
+
10
+distances = {}
11
+places = {}
12
+
13
+for line in input.splitlines():
14
+    (source, dest, distance) = re.match('^(.*?) to (.*?) = ([0-9]+)$', line).group(1, 2, 3)
15
+    distances[(source, dest)] = int(distance)
16
+    distances[(dest, source)] = int(distance)
17
+    places[dest] = True
18
+    places[source] = True
19
+
20
+places = places.keys()
21
+
22
+
23
+def without(list, item):
24
+    return [x for x in list if x != item]
25
+
26
+
27
+def find_shortest(current, to_visit):
28
+    if len(to_visit) == 0:
29
+        return 0
30
+
31
+    return FUNC([distances[(current, target)] +
32
+                find_shortest(target, without(to_visit, target))
33
+                for target in to_visit])
34
+
35
+print(FUNC([find_shortest(place, without(places, place)) for place in places]))

+ 28
- 0
9.txt View File

@@ -0,0 +1,28 @@
1
+Faerun to Norrath = 129
2
+Faerun to Tristram = 58
3
+Faerun to AlphaCentauri = 13
4
+Faerun to Arbre = 24
5
+Faerun to Snowdin = 60
6
+Faerun to Tambi = 71
7
+Faerun to Straylight = 67
8
+Norrath to Tristram = 142
9
+Norrath to AlphaCentauri = 15
10
+Norrath to Arbre = 135
11
+Norrath to Snowdin = 75
12
+Norrath to Tambi = 82
13
+Norrath to Straylight = 54
14
+Tristram to AlphaCentauri = 118
15
+Tristram to Arbre = 122
16
+Tristram to Snowdin = 103
17
+Tristram to Tambi = 49
18
+Tristram to Straylight = 97
19
+AlphaCentauri to Arbre = 116
20
+AlphaCentauri to Snowdin = 12
21
+AlphaCentauri to Tambi = 18
22
+AlphaCentauri to Straylight = 91
23
+Arbre to Snowdin = 129
24
+Arbre to Tambi = 53
25
+Arbre to Straylight = 40
26
+Snowdin to Tambi = 15
27
+Snowdin to Straylight = 99
28
+Tambi to Straylight = 70

Loading…
Cancel
Save