-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
72 lines (60 loc) · 1.85 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
70
71
72
from itertools import cycle
from time import perf_counter
from os.path import basename
from statistics import median
program_name = basename(__file__).rstrip(".py")
with open(f"./input/{program_name}.in") as f:
data = f.read().splitlines()
n_lines = len(data)
data = cycle(data)
def solve_part1():
open_chars = ["(", "[", "{", "<"]
close_chars = [")", "]", "}", ">"]
open2close = dict(zip(open_chars, close_chars))
score_dict = dict(zip(close_chars, [3, 57, 1197, 25137]))
score = 0
for _ in range(n_lines):
line = next(data)
opened = [line[0]]
for char in line[1:]:
if char in open_chars:
opened.append(char)
elif open2close[opened[-1]] == char:
opened.pop()
else:
score += score_dict[char]
break
return score
def solve_part2():
lines = [next(data) for _ in range(n_lines)]
open_chars = ["(", "[", "{", "<"]
close_chars = [")", "]", "}", ">"]
open2close = dict(zip(open_chars, close_chars))
incomplete = []
for line in lines:
opened = [line[0]]
for char in line[1:]:
if char in open_chars:
opened.append(char)
elif open2close[opened[-1]] == char:
opened.pop()
else:
break
else:
rem = map(open2close.get, reversed(opened))
incomplete.append(rem)
score_dict = dict(zip(close_chars, [1, 2, 3, 4]))
scores = []
for rem in incomplete:
score = 0
for char in rem:
score *= 5
score += score_dict[char]
scores.append(score)
return median(scores)
start = perf_counter()
s1 = solve_part1()
print("Solution 1:", s1)
s2 = solve_part2()
print("Solution 2:", s2)
print("Execution took", perf_counter() - start, "s")