-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
75 lines (65 loc) · 1.47 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
73
74
75
# https://adventofcode.com/2021/day/10
from aocd import data, submit
from functools import reduce
_data = '''[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]'''
lines = data.splitlines()
open_bracket = '([<{'
expect = {
'(': ')',
'[': ']',
'<': '>',
'{': '}'
}
def parse(line):
buf = []
for char in line:
if char in open_bracket:
buf.append(char)
else:
expected = expect[buf.pop()]
if char != expected:
return -2, expected, char
if len(buf) > 0:
buf.reverse()
return -1, buf, None
return 0, None, None
def solve1():
scores = {
')': 3,
']': 57,
'}': 1197,
'>': 25137
}
accu = 0
for i in lines:
code, expected, actual = parse(i)
if code == -2:
accu += scores[actual]
submit(accu, part='a')
def solve2():
score_map = {
')': 1,
']': 2,
'}': 3,
'>': 4
}
scores = []
for i in lines:
code, buf, __ = parse(i)
if code == -1:
score = reduce(lambda r, j: r * 5 + score_map[j], [expect[i] for i in buf], 0)
scores.append(score)
scores.sort()
middle = scores[int(len(scores) / 2)]
submit(middle, part='b')
solve1()
solve2()