Day 10 (glamorized)
This commit is contained in:
@@ -5,18 +5,18 @@ lines = (x.strip() for x in open("input.txt"))
|
|||||||
|
|
||||||
cnt = []
|
cnt = []
|
||||||
OPEN, CLOSE, PENALTY = "([{<", ")]}>", [1, 2, 3, 4]
|
OPEN, CLOSE, PENALTY = "([{<", ")]}>", [1, 2, 3, 4]
|
||||||
MAP_CLOSE, MAP_PENALTY = dict(zip(OPEN, CLOSE)), dict(zip(OPEN, PENALTY))
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
success = True
|
try:
|
||||||
stack = []
|
stack = []
|
||||||
for char in line:
|
for char in line:
|
||||||
if char in OPEN:
|
try:
|
||||||
stack.append(char)
|
stack.append(OPEN.index(char))
|
||||||
elif MAP_CLOSE[stack.pop()] != char:
|
except ValueError:
|
||||||
success = False
|
if CLOSE[stack.pop()] != char:
|
||||||
break
|
raise ValueError("Char not found: " + char)
|
||||||
if success:
|
cnt.append(reduce(lambda p, c: p*5 + PENALTY[c], stack[::-1], 0))
|
||||||
cnt.append(reduce(lambda p, c: p*5 + MAP_PENALTY[c], stack[::-1], 0))
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
cnt.sort()
|
cnt.sort()
|
||||||
print(cnt[len(cnt) // 2])
|
print(cnt[len(cnt) // 2])
|
||||||
|
|||||||
Reference in New Issue
Block a user