-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
75 lines (62 loc) · 1.35 KB
/
day10.py
File metadata and controls
75 lines (62 loc) · 1.35 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
69
70
71
72
73
74
75
pairs = {
'(': ')',
'[': ']',
'{': '}',
'<': '>'
}
points1 = {
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
points2 = {
')': 1,
']': 2,
'}': 3,
'>': 4,
}
def parse(input):
return input.splitlines()
def part1(input):
input = parse(input)
ret = 0
for line in input:
stack = []
for char in line:
if char in pairs:
stack.append(pairs[char])
continue
if char != stack[-1]:
ret += points1[char]
break
stack.pop()
return ret
def part2(input):
input = parse(input)
scores = []
for line in input:
stack = []
for char in line:
if char in pairs:
stack.append(pairs[char])
continue
if char != stack[-1]:
break
stack.pop()
else:
score = 0
for char in reversed(stack):
score *= 5
score += points2[char]
scores.append(score)
scores.sort()
return scores[len(scores)//2]
if __name__ == '__main__':
import sys
import utils
input = sys.stdin.read()
p1, t1 = utils.time(part1, input)
print(f'part1: {p1} ({t1:.20f})')
p2, t2 = utils.time(part2, input)
print(f'part2: {p2} ({t2:.20f})')