#!/usr/bin/env python3 from functools import reduce lines = (x.strip() for x in open("input.txt")) cnt = [] OPEN, CLOSE, PENALTY = "([{<", ")]}>", [1, 2, 3, 4] for line in lines: try: stack = [] for char in line: try: stack.append(OPEN.index(char)) except ValueError: if CLOSE[stack.pop()] != char: raise ValueError("Char not found: " + char) cnt.append(reduce(lambda p, c: p*5 + PENALTY[c], stack[::-1], 0)) except ValueError: pass cnt.sort() print(cnt[len(cnt) // 2])