+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDay 105191eec

master
817 B27 linesraw
1from collections import deque
2from functools import reduce as fold
3
4table = {")": 3, "]": 57, "}": 1197, ">": 25137}
5othertable = {c: i + 1 for i, c in enumerate(table)}
6match = {"(": ")", "{": "}", "<": ">", "[": "]"}
7total = 0
8scores = []
9with open("input") as f:
10    for line in f:
11        stack = deque()
12        skip = False
13        for c in line.strip():
14            if c in match:
15                stack.append(c)
16            else:
17                off = stack.pop()
18                if c != match[off]:
19                    total += table[c]
20                    skip = True
21                    break
22        if skip:
23            continue
24        close = [match[c] for c in stack][::-1]
25        scores.append(fold(lambda a, c: 5 * a + othertable[c], close, 0))
26print(total)
27print(sorted(scores)[len(scores) // 2])