#!/usr/bin/env python3 import sys from collections import defaultdict match = { "}": "{", ")": "(", ">": "<", "]": "[", } rmatch = { "{": "}", "(": ")", "<": ">", "[": "]", } pts = { ")": 1, "]": 2, "}": 3, ">": 4, } def score(seq): score = 0 for c in seq: score *= 5 score += pts[c] return score scores = [] for l in sys.stdin: mem = [] corrupt = False for c in l.strip(): if c in ["{", "(", "<", "["]: mem.append(c) elif c in ["}", ")", ">", "]"]: m = match[c] if mem.pop() != m: # this is an corrupt line, skip it! corrupt = True break else: raise Exception(f"Bad char: {c}") # Figure out and score the remaining chars! if not corrupt: mem.reverse() remaining = [rmatch[c] for c in mem] scores.append(score(remaining)) ss = sorted(scores) print(ss[int(len(ss) / 2)])