summaryrefslogtreecommitdiff
path: root/10_2021.py
blob: 539ee7e03370ef0736d7a1760df88fd8302a5018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from collections import defaultdict, Counter, deque

SCORES = []
ans = 0
data = sys.argv[1] if len(sys.argv) > 1 else "day10data.txt"
for line in open(data):
    bad = False
    S = deque()
    for c in line.strip():
        if c in ["(", "[", "{", "<"]:
            S.append(c)
        elif c == ")":
            if S[-1] != "(":
                ans += 3
                bad = True
                break
            else:
                S.pop()
        elif c == "]":
            if S[-1] != "[":
                ans += 57
                bad = True
                break
            else:
                S.pop()
        elif c == "}":
            if S[-1] != "{":
                ans += 1197
                bad = True
                break
            else:
                S.pop()
        elif c == ">":
            if S[-1] != "<":
                ans += 25137
                bad = True
                break
            else:
                S.pop()
    if not bad:
        score = 0
        P = {"(": 1, "[": 2, "{": 3, "<": 4}
        for c in reversed(S):
            score = score * 5 + P[c]
        SCORES.append(score)
print(ans)
SCORES.sort()
print(SCORES[len(SCORES) // 2])