-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
43 lines (35 loc) · 1014 Bytes
/
10.py
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
from collections import Counter
from functools import reduce
from statistics import median
from utils import iter_file, iter_lines, rinput
left, right = "([{<", ")]}>"
pairs = dict(zip(left, right))
def solve1(lines):
s = []
table = dict(zip(right, (3, 57, 1197, 25137)))
error_score = 0
for line in lines:
for ch in line:
if ch in left:
s.append(ch)
else:
if pairs[s.pop()] != ch:
error_score += table[ch]
return error_score
def solve2(lines):
scores = []
table = dict(zip(left, range(1, 5)))
for line in lines:
s = []
for ch in line:
if ch in left:
s.append(ch)
elif pairs[s.pop()] != ch:
break
else:
scores.append(
reduce(lambda score, val: score * 5 + table[val], reversed(s), 0)
)
return median(scores)
print(solve1(iter_file(10)))
print(solve2(iter_file(10)))