From bde8b1b48f667d528b537e398b55a5b8b4509796 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 10 Dec 2021 09:57:20 +0000 Subject: day 10 --- 10.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 10.py 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)) -- cgit v1.2.3-54-g00ecf