47 lines
646 B
Python
47 lines
646 B
Python
with open("input.txt") as f:
|
|
lines = [l.strip() for l in f.readlines()]
|
|
|
|
|
|
pairs = {
|
|
"(": ")",
|
|
"[": "]",
|
|
"{": "}",
|
|
"<": ">",
|
|
}
|
|
|
|
antipairs = {
|
|
")": "(",
|
|
"]": "[",
|
|
"}": "{",
|
|
">": "<",
|
|
}
|
|
|
|
openers = set("([{<")
|
|
closers = set(")]}>")
|
|
|
|
scores = {
|
|
")": 3,
|
|
"]": 57,
|
|
"}": 1197,
|
|
">": 25137,
|
|
}
|
|
|
|
|
|
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 scores[c]
|
|
return 0
|
|
|
|
|
|
score = 0
|
|
for line in lines:
|
|
score += process_line(line)
|
|
|
|
print(score)
|