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))