-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
48 lines (38 loc) · 1.08 KB
/
day10.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
44
45
46
47
48
from functools import reduce
with open("input10.txt", 'r') as file:
brackets = [i.strip() for i in file.readlines()]
def analysis(synt):
opening = ['(', '[', '{', '<']
closing = [')', ']', '}', '>']
stack = []
for j in synt:
if j in opening:
stack.append(j)
elif closing[opening.index(stack.pop())] != j:
return (True, j)
return (False, ''.join(stack))
"""
sm = 0
score = {')': 3, "]": 57, "}": 1197, ">": 25137}
for i in brackets:
q = analysis(i)
if q[0]:
sm += score[q[1]]
print(sm)
"""
incomplete = [analysis(i)[1] for i in brackets if not analysis(i)[0]]
def closed(i):
ans = ''
for j in i[::-1]:
if j == '(':
ans += ')'
if j == '[':
ans += ']'
if j == '{':
ans += '}'
if j == '<':
ans += '>'
return ans
scores = [reduce(lambda x, y: 5*x + {')': 1, ']': 2, '}': 3,
'>': 4}.get(y), closed(i), 0) for i in incomplete]
print(sorted(scores)[len(scores)//2])