summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-09 23:13:41 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-09 23:13:41 -0800
commitf9e4204dd9b107174ad2d6f31c7a5068b2fe40da (patch)
tree0195f154f960aab9078252aa5c17740a32ecc9e9
parentb9b5574179dcbb5f6d3fa0715905a047d1cd377e (diff)
downloadAdventOfCode2021-f9e4204dd9b107174ad2d6f31c7a5068b2fe40da.tar.gz
solution for day 10
-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])