From 9db0c549e4a5d7e21ae6e62619ef3521e0e5119a Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Fri, 10 Dec 2021 11:13:52 +0100 Subject: [PATCH] Day 10 (glamorized) --- day10/part2.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/day10/part2.py b/day10/part2.py index e8bd3ba..8d7d1b2 100644 --- a/day10/part2.py +++ b/day10/part2.py @@ -5,18 +5,18 @@ lines = (x.strip() for x in open("input.txt")) cnt = [] OPEN, CLOSE, PENALTY = "([{<", ")]}>", [1, 2, 3, 4] -MAP_CLOSE, MAP_PENALTY = dict(zip(OPEN, CLOSE)), dict(zip(OPEN, PENALTY)) for line in lines: - success = True - stack = [] - for char in line: - if char in OPEN: - stack.append(char) - elif MAP_CLOSE[stack.pop()] != char: - success = False - break - if success: - cnt.append(reduce(lambda p, c: p*5 + MAP_PENALTY[c], stack[::-1], 0)) + try: + stack = [] + for char in line: + try: + stack.append(OPEN.index(char)) + except ValueError: + if CLOSE[stack.pop()] != char: + raise ValueError("Char not found: " + char) + cnt.append(reduce(lambda p, c: p*5 + PENALTY[c], stack[::-1], 0)) + except ValueError: + pass cnt.sort() print(cnt[len(cnt) // 2])