aoc/2021/10/10.py

54 lines
1,018 B
Python
Raw Normal View History

2021-12-18 16:40:06 +00:00
with open("input.txt") as f:
lines = f.read().split('\n')
score = 0
incomplete = []
for line in lines:
corrupt = False
stack = []
for char in line:
if char in ['(','[','{','<']:
stack.append(char)
elif char == ')':
prev = stack.pop()
if prev != '(':
score += 3
corrupt = True
break
elif char == ']':
prev = stack.pop()
if prev != '[':
score += 57
corrupt = True
break
elif char == '}':
prev = stack.pop()
if prev != '{':
score += 1197
corrupt = True
break
elif char == '>':
prev = stack.pop()
if prev != '<':
score += 25137
corrupt = True
break
if not corrupt:
incomplete.append(stack)
print(score)
scores = []
for stack in incomplete:
score = 0
while stack:
prev = stack.pop()
if prev == '(':
score = 5*score + 1
elif prev == '[':
score = 5*score + 2
elif prev == '{':
score = 5*score + 3
elif prev == '<':
score = 5*score + 4
scores.append(score)
from statistics import median
print(median(scores))