-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
46 lines (40 loc) · 898 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
match_parenthesis = {
"(":")",
"{":"}",
"[":"]",
"<":">"
}
maping_parenthesis = {
"}":"{",
")":"(",
"]":"[",
">":"<"
}
corrupted_score = {
")": 3,
"]": 57,
"}": 1197,
">": 25137
}
incomplete_score = {")": 1, "]": 2, "}": 3, ">": 4}
with open("input/day10.txt") as f:
data = f.readlines()
answer1 = 0
answer2_candidates = []
for line in data:
stack = []
for c in line.strip():
if c in ")}]>":
if stack.pop() != maping_parenthesis[c]:
answer1 += corrupted_score[c]
break
else:
stack.append(c)
else:
score = 0
for c in reversed(stack):
score = score * 5 + incomplete_score[match_parenthesis[c]]
answer2_candidates.append(score)
answer2 = sorted(answer2_candidates)[len(answer2_candidates) // 2]
print(answer1)
print(answer2)