This commit is contained in:
Sebastian Seedorf
2021-12-10 10:57:14 +01:00
parent 8efc7412a7
commit 30e7bec871
3 changed files with 149 additions and 0 deletions

22
day10/part2.py Normal file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python3
from functools import reduce
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))
cnt.sort()
print(cnt[len(cnt) // 2])