-
Notifications
You must be signed in to change notification settings - Fork 1
/
day10.py
69 lines (56 loc) · 1.5 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
PAIRS: dict[str, str] = {
"(": ")",
"[": "]",
"{": "}",
"<": ">",
}
def problem1(navigation: list[str]) -> int:
VALUES: dict[str, int] = {
")": 3,
"]": 57,
"}": 1197,
">": 25137,
}
t: int = 0
for line in navigation:
stack: list[str] = []
for char in line:
if char in PAIRS.keys():
stack.append(PAIRS[char])
elif char == stack[-1]:
stack.pop()
else:
t += VALUES[char]
break
return t
def problem2(navigation: list[str]) -> int:
VALUES: dict[str, int] = {
")": 1,
"]": 2,
"}": 3,
">": 4,
}
completions: list[int] = []
for i, line in enumerate(navigation):
corrupt: bool = False
stack: list[str] = []
for char in line:
if char in PAIRS.keys():
stack.append(PAIRS[char])
elif char == stack[-1]:
stack.pop()
else:
corrupt = True
break
if not corrupt:
t: int = 0
for char in stack[::-1]:
t = (t * 5) + VALUES[char]
completions.append(t)
completions.sort()
return completions[round((len(completions) - 1) / 2)]
if __name__ == "__main__":
with open("input10.txt") as f:
data: list[str] = f.read().splitlines()
print("solution 1:", problem1(data))
print("solution 2:", problem2(data))