10_syntax.py 638 B

1234567891011121314151617181920212223
  1. #!/usr/bin/env python3
  2. import sys
  3. from statistics import median
  4. CLOSE = {'(': ')', '[': ']', '{': '}', '<': '>'}
  5. CORRUPT = {')': 3, ']': 57, '}': 1197, '>': 25137}
  6. def complete(chunk):
  7. expect = []
  8. for char in chunk:
  9. if char in CLOSE:
  10. expect.append(CLOSE[char])
  11. elif char != expect.pop():
  12. return -CORRUPT[char]
  13. score = 0
  14. for char in reversed(expect):
  15. score = score * 5 + ' )]}>'.index(char)
  16. return score
  17. scores = [complete(line.rstrip()) for line in sys.stdin]
  18. print(sum(-score for score in scores if score < 0))
  19. print(median(score for score in scores if score > 0))