summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--day10.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/day10.py b/day10.py
new file mode 100644
index 0000000..5cb1073
--- /dev/null
+++ b/day10.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+
+from collections import deque
+
+pairs = {
+    "(" : ")",
+    "[" : "]",
+    "{" : "}",
+    "<" : ">"
+}
+
+scores = {
+    ")": 3,
+    "]": 57,
+    "}": 1197,
+    ">": 25137
+}
+
+scores2 = {
+    ")": 1,
+    "]": 2,
+    "}": 3,
+    ">": 4
+}
+
+# part 1
+score = 0
+completion_scores = []
+with open("day10.txt") as data:
+    for line in data:
+        stack = deque()
+        invalid = False
+        for char in line.strip():
+            if char in pairs.keys():
+                stack.append(char)
+            if char in pairs.values():
+                if len(stack) == 0 or pairs[stack[-1]] != char:
+                    score += scores[char]
+                    invalid = True
+                    break
+                elif pairs[stack[-1]] == char:
+                    stack.pop()
+        # part 2
+        if not invalid:
+            total = 0
+            while stack:
+                total = (5 * total) + scores2[pairs[stack.pop()]]
+            completion_scores.append(total)
+
+print(score)
+completion_scores.sort()
+print(completion_scores[(len(completion_scores) - 1) // 2])