-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.py
81 lines (66 loc) · 1.77 KB
/
day_10.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
76
77
78
79
80
81
import aoc_helper
from aoc_helper import extract_ints, frange, irange, iter, list, map, range, tail_call
raw = aoc_helper.fetch(10, 2021)
def parse_raw():
return list(raw.splitlines())
data = parse_raw()
def parse_corrupt(line):
stack = []
for c in line:
if c == "(":
stack.append(c)
elif c == ")":
if stack.pop() != "(":
return c
elif c == "[":
stack.append(c)
elif c == "]":
if stack.pop() != "[":
return c
elif c == "{":
stack.append(c)
elif c == "}":
if stack.pop() != "{":
return c
elif c == "<":
stack.append(c)
elif c == ">":
if stack.pop() != "<":
return c
return ""
def part_one():
return (
data.mapped(parse_corrupt)
.filtered(None)
.mapped(lambda i: {")": 3, "]": 57, "}": 1197, ">": 25137}[i])
.sum()
)
def autocomplete(line):
stack = []
for c in line:
if c == "(":
stack.append(c)
elif c == ")":
stack.pop()
elif c == "[":
stack.append(c)
elif c == "]":
stack.pop()
elif c == "{":
stack.append(c)
elif c == "}":
stack.pop()
elif c == "<":
stack.append(c)
elif c == ">":
stack.pop()
stack = stack[::-1]
score = 0
for c in stack:
score *= 5
score += " ([{<".index(c)
return score
def part_two():
return data.filtered(lambda i: parse_corrupt(i) == "").mapped(autocomplete).median()
aoc_helper.lazy_submit(day=10, year=2021, solution=part_one)
aoc_helper.lazy_submit(day=10, year=2021, solution=part_two)