-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha10.py
74 lines (56 loc) · 1.65 KB
/
a10.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
from functools import reduce
example = """
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
""".strip()
f = open("input/10.input")
text = f.read().strip()
def parse(t):
return t.split("\n")
opening = ['(', "[", '<', '{']
closing = [')', "]", '>', '}']
scores_corrupted = {')': 3, ']': 57, '}': 1197, '>': 25137}
scores_complete = {'(': 1, '[': 2, '{': 3, '<': 4}
def corrupted_char(line):
found = []
for c in line:
if c in opening:
found.append(c)
elif c in closing:
last_opened = found.pop()
idx_open = opening.index(last_opened)
idx_close = closing.index(c)
if idx_open != idx_close:
return c, None
return None, found
def part1(text):
parsed = parse(text)
return sum(
map(scores_corrupted.get,
filter(lambda c: c is not None,
map(lambda line: corrupted_char(line)[0], parsed))))
def complete_score(remaining):
return reduce(lambda acc, opened: acc * 5 + scores_complete[opened], reversed(remaining), 0)
def part2(text):
parsed = parse(text)
scores = sorted(
map(complete_score,
filter(lambda l: l is not None,
map(lambda line: corrupted_char(line)[1], parsed)))
)
return scores[len(scores) // 2]
def main(text):
print("Part1:", part1(text))
print("Part2:", part2(text))
print("==== EXAMPLES ====")
main(example)
print("==== SOLUTION RESULT ====")
main(text)