aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

a.py (632B) [raw]


      1 #!/usr/bin/env python3
      2 import sys
      3 from collections import defaultdict
      4 
      5 match = {
      6     "}": "{",
      7     ")": "(",
      8     ">": "<",
      9     "]": "[",
     10 }
     11 
     12 bad = defaultdict(int)
     13 for l in sys.stdin:
     14     mem = []
     15     for c in l.strip():
     16         if c in ["{", "(", "<", "["]:
     17             mem.append(c)
     18         elif c in ["}", ")", ">", "]"]:
     19             m = match[c]
     20             if mem.pop() != m:
     21                 bad[c] += 1
     22                 break
     23         else:
     24             raise Exception(f"Bad char: {c}")
     25 
     26 # ): 3 points.
     27 # ]: 57 points.
     28 # }: 1197 points.
     29 # >: 25137 points.
     30 print(bad["}"] * 1197 + bad[")"] * 3 + bad[">"] * 25137 + bad["]"] * 57)