-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10_task2.py
More file actions
68 lines (54 loc) · 1.54 KB
/
Copy pathday10_task2.py
File metadata and controls
68 lines (54 loc) · 1.54 KB
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
###############################################################################
# Day 10, Task 1 #
###############################################################################
import aoc_util
day = 10
data_str = """[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]"""
def score_sign(s):
if s == ")":
return 1
if s == "]":
return 2
if s == "}":
return 3
if s == ">":
return 4
def task(data_set: list[str]) -> int:
scores = []
for line in data_set:
chars = []
corrupted = False
for i in line.replace("\n", ""):
if i == "(":
chars.append(")")
elif i == "[":
chars.append("]")
elif i == "{":
chars.append("}")
elif i == "<":
chars.append(">")
else:
last = chars.pop()
if last != i:
corrupted = True
break
if not corrupted:
chars.reverse()
score = 0
for i in chars:
score *= 5
score += score_sign(i)
scores.append(score)
scores.sort()
return scores[len(scores) // 2]
aoc_util.run_with_data_str(task, data_str)
aoc_util.run_with_data_set(task, day)