summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-10 09:57:20 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-10 09:57:36 +0000
commitbde8b1b48f667d528b537e398b55a5b8b4509796 (patch)
tree51208da678b2893a0e32a5792cd478046e3b5145
parent9f8c9890c3a037ed4b69a041e39f96af54f84a3f (diff)
downloadaoc2021-bde8b1b48f667d528b537e398b55a5b8b4509796.tar.gz
aoc2021-bde8b1b48f667d528b537e398b55a5b8b4509796.tar.xz
aoc2021-bde8b1b48f667d528b537e398b55a5b8b4509796.zip
day 10
-rw-r--r--10.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/10.py b/10.py
new file mode 100644
index 0000000..2eb8c76
--- /dev/null
+++ b/10.py
@@ -0,0 +1,46 @@
+from dataclasses import dataclass
+from enum import Enum
+from statistics import median
+from utils import open_day
+
+@dataclass
+class Chunk:
+ begin: str
+ end: str
+ error_score: int
+ completion_score: int
+
+class ChunkType(Enum):
+ PAREN = Chunk('(', ')', 3, 1)
+ SQUARE = Chunk('[', ']', 57, 2)
+ CURLY = Chunk('{', '}', 1197, 3)
+ ANGLE = Chunk('<', '>', 25137, 4)
+
+opening = { e.value.begin: e for e in ChunkType }
+closing = { e.value.end: e for e in ChunkType }
+
+error_score = 0
+completion_scores = []
+with open_day(10) as f:
+ for line in f:
+ line = line.rstrip()
+ stack = []
+ for c in line:
+ if c in opening:
+ stack.append(opening[c])
+ else:
+ expect = stack.pop()
+ got = closing[c]
+ if got != expect:
+ error_score += got.value.error_score
+ break
+ else:
+ score = 0
+ while stack:
+ completion = stack.pop()
+ score *= 5
+ score += completion.value.completion_score
+ completion_scores.append(score)
+
+print(error_score)
+print(median(completion_scores))