-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_10.py
69 lines (56 loc) · 1.32 KB
/
problem_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
data_path = "data/problem_10.txt"
# data_path = "data/problem_10_test.txt"
with open(data_path, "r") as f:
data = f.readlines()
# part 1
bracket_pairs = {
"(": ")",
"[": "]",
"{": "}",
"<": ">",
}
error_scores = {
")": 3,
"]": 57,
"}": 1197,
">": 25137,
}
autocomplete_scores = {
")": 1,
"]": 2,
"}": 3,
">": 4,
}
score = 0
for line in data:
stack = []
for char in line.rstrip():
if not stack or char in bracket_pairs:
stack.append(char)
elif char != bracket_pairs[stack[-1]]:
score += error_scores[char]
break
else:
stack.pop()
print(f"Part 1 solution: {score}")
# part 2
scores = []
for line in data:
score = 0
stack = []
corrupt_line = False
for char in line.rstrip():
if not stack or char in bracket_pairs:
stack.append(char)
elif char != bracket_pairs[stack[-1]]:
corrupt_line = True
break
else:
stack.pop()
if not corrupt_line and stack:
missing_brackets = (bracket_pairs[char] for char in reversed(stack))
for char in missing_brackets:
score = (score * 5) + autocomplete_scores[char]
scores.append(score)
scores.sort()
print(f"Part 2 solution: {scores[len(scores) // 2]}")