-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay10.py
52 lines (42 loc) · 951 Bytes
/
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
49
50
51
52
from sys import argv, stdin
ls = stdin.read().splitlines(False)
scores = {")": 3, "]": 57, "}": 1197, ">": 25137}
matching = ["()", "[]", "{}", "<>"]
ps = {}
init = set()
end = set()
for c1, c2 in matching:
init.add(c1)
end.add(c2)
ps[c1] = c2
ps[c2] = c1
incomplete = []
total = 0
for l in ls:
stack = [None]
ending = None
for c in l:
if c in init:
stack.append(c)
elif c in end and stack[-1] == ps[c]:
stack.pop()
else:
ending = c
break
if ending:
total += scores[ending]
else:
incomplete.append([l, stack])
if argv[1] == "1":
print(total)
else:
ps2 = ")]}>"
scores = []
for _, rem in incomplete:
result = 0
for c in rem[1:][::-1]:
result *= 5
result += ps2.index(ps[c]) + 1
scores.append(result)
scores.sort()
print(scores[len(scores) // 2])