aoc2021/d10/d10_2.py

29 lines
567 B
Python
Raw Normal View History

2021-12-20 19:04:33 +00:00
import sys
from collections import deque
D = { ">":"<", ")":"(", "]":"[","}":"{", }
2021-12-20 21:41:29 +00:00
S = { "(":1, "[":2, "{":3, "<":4, }
2021-12-20 19:04:33 +00:00
L = sys.stdin.read().splitlines()
N = []
for l in L:
2021-12-20 21:41:29 +00:00
Q = deque(["y"]) # add buffer to pop
2021-12-20 19:04:33 +00:00
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:
2021-12-20 21:41:29 +00:00
R = list(Q)[1:]
2021-12-20 19:04:33 +00:00
s = 0
for r in reversed(R):
s = s*5 + S[r]
N.append(s)
print(sorted(N)[len(N)//2])