From aa773e761e8f088c1f301d7067ba38b99f152cc3 Mon Sep 17 00:00:00 2001 From: Marijn Doeve Date: Fri, 10 Dec 2021 16:02:29 +0100 Subject: [PATCH] Better Stack --- 10/10.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/10/10.py b/10/10.py index 491b52b..3f43276 100644 --- a/10/10.py +++ b/10/10.py @@ -1,6 +1,3 @@ -from typing import Optional - - class InvalidChar(Exception): pass @@ -55,20 +52,21 @@ def part_two(lines: list[str]) -> int: score = 0 try: - stack: list[Optional[str]] = [None] + stack: list[str] = [] for char in list(line): if char in CLOSING_FOR_OPEN.values(): # Open backets stack.append(char) elif char in CLOSING_FOR_OPEN: # Close brackets top = stack.pop() + if top != CLOSING_FOR_OPEN[char]: # Ignore invalid lines raise InvalidChar # Close unclosed pairs - while top := stack.pop(): - score = score * 5 + SCORE_TWO[top] + while stack: + score = score * 5 + SCORE_TWO[stack.pop()] scores.append(score)