67 lines
962 B
Python
67 lines
962 B
Python
with open("input.txt") as f:
|
|
lines = [l.strip() for l in f.readlines()]
|
|
|
|
|
|
pairs = {
|
|
"(": ")",
|
|
"[": "]",
|
|
"{": "}",
|
|
"<": ">",
|
|
}
|
|
|
|
antipairs = {
|
|
")": "(",
|
|
"]": "[",
|
|
"}": "{",
|
|
">": "<",
|
|
}
|
|
|
|
openers = set("([{<")
|
|
closers = set(")]}>")
|
|
|
|
scores = {
|
|
")": 1,
|
|
"]": 2,
|
|
"}": 3,
|
|
">": 4,
|
|
}
|
|
|
|
|
|
def calc_score(chars):
|
|
score = 0
|
|
for c in chars:
|
|
score = score * 5
|
|
score += scores[pairs[c]]
|
|
return score
|
|
|
|
|
|
def process_line(line):
|
|
stack = []
|
|
for c in line:
|
|
if c in openers:
|
|
stack.append(c)
|
|
else:
|
|
x = stack.pop()
|
|
if antipairs[c] != x:
|
|
return 0 # invalid line
|
|
stack.reverse()
|
|
return calc_score(stack)
|
|
|
|
|
|
line_scores = []
|
|
|
|
for line in lines:
|
|
s = process_line(line)
|
|
if s:
|
|
line_scores.append(s)
|
|
|
|
|
|
line_scores.sort()
|
|
|
|
print(line_scores)
|
|
print(len(line_scores))
|
|
|
|
print(line_scores[len(line_scores) // 2])
|
|
|
|
# 22m
|