41 lines
1 KiB
Python
41 lines
1 KiB
Python
#!/usr/bin/env python3
|
|
import fileinput
|
|
from collections import deque
|
|
from timeit import timeit
|
|
from typing import Tuple
|
|
|
|
lines = list(fileinput.input())
|
|
|
|
opposites = {"[": "]", "(": ")", "{": "}", "<": ">"}
|
|
part1_points = {")": 3, "]": 57, "}": 1197, ">": 25137}
|
|
part2_points = {"(": 1, "[": 2, "{": 3, "<": 4}
|
|
|
|
def main() -> Tuple[int, int]:
|
|
part1 = 0
|
|
part2_scores = []
|
|
|
|
for line_ in lines:
|
|
line = str(line_).strip()
|
|
stack = deque()
|
|
for c in line:
|
|
if c in "[({<":
|
|
stack.appendleft(c)
|
|
elif opposites[stack[0]] != c:
|
|
part1 += part1_points[c]
|
|
break
|
|
else:
|
|
stack.popleft()
|
|
else:
|
|
score = 0
|
|
for c in stack:
|
|
score = score * 5 + part2_points[c]
|
|
part2_scores.append(score)
|
|
|
|
part2_scores.sort()
|
|
part2 = part2_scores[len(part2_scores) // 2]
|
|
|
|
return (part1, part2)
|
|
|
|
if __name__ == "__main__":
|
|
print(main())
|
|
print(f"{timeit(main, number=1000):.3f} ms")
|