summary refs log tree commit diff stats
path: root/day12.py
diff options
context:
space:
mode:
Diffstat (limited to 'day12.py')
-rw-r--r--day12.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/day12.py b/day12.py
new file mode 100644
index 0000000..55732b5
--- /dev/null
+++ b/day12.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+from collections import defaultdict
+
+graph = defaultdict(list)
+with open("day12.txt") as data:
+    for line in data:
+        vert1, vert2 = line.strip().split('-')
+        if vert2 != "start":
+            graph[vert1].append(vert2)
+        if vert1 != "start":
+            graph[vert2].append(vert1)
+    del graph["end"]
+
+# part 1
+def walk(path=['start']):
+    count = 0
+    for point in graph[path[-1]]:
+        if point.isupper() or not point in path:
+            count += 1 if point == 'end' else walk(path + [point])
+    return count
+
+print(walk())
+
+# part 2
+def walk2(path=['start']):
+    count = 0
+    for point in graph[path[-1]]:
+        count += 1 if point == 'end' else (walk2,
+                                           walk)[point.islower()
+                                                 and point in path](
+                                                     path + [point])
+    return count
+
+print(walk2())