aoc2021/d10/d10_2.py

29 lines
554 B
Python
Raw Normal View History

2021-12-20 19:04:33 +00:00
import sys
from collections import deque
D = { ">":"<", ")":"(", "]":"[","}":"{", }
S = { "(":1, "[":2, "{":3, "<":4, }
L = sys.stdin.read().splitlines()
N = []
for l in L:
Q = deque(["y"]*120)
fail = False
for c in l:
p = Q.pop()
if c in D: # if closing char
e = D[c]
if p != e: # fail case
fail = True
break
else: # just discard the pair
pass
else: # not closing
Q.append(p)
Q.append(c)
if not fail:
R = list(Q)[120:]
s = 0
for r in reversed(R):
s = s*5 + S[r]
N.append(s)
print(sorted(N)[len(N)//2])