-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday10.py
75 lines (68 loc) · 1.8 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
SAMPLE = '''[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
'''
def parse_txt(txt):
return txt.strip().split('\n')
def part1(txt):
"""
>>> part1(SAMPLE)
26397
"""
lines = parse_txt(txt)
scores = dict(zip(')]}>', [3, 57, 1197, 25_137]))
score = 0
for line in lines:
results = find_illegal_move(line)
if results[0]:
expected, got, remaining = results
score += scores[got]
return score
def part2(txt):
"""
>>> part2(SAMPLE)
288957
"""
lines = parse_txt(txt)
scores = dict(zip(')]}>', [1, 2, 3, 4]))
all_scores = []
for line in lines:
score = 0
expected, got, remaining = find_illegal_move(line)
if got:
continue
for char in remaining:
score *=5
score += scores.get(char, 0)
#print(remaining, score)
all_scores.append(score)
return sorted(all_scores)[(len(all_scores)-1)//2]
def find_illegal_move(line):
"""
Return expected, got, remaining
None if good
>>> find_illegal_move('[)')
(']', ')', ']')
"""
expected = []
mapping = dict(zip('[<({', ']>)}'))
for char in line:
if next_char := mapping.get(char):
expected.append(next_char)
elif char != expected[-1]:
return expected[-1], char, ''.join(expected)[::-1]
else:
expected.pop()
return None, None, ''.join(expected)[::-1]
if __name__ == '__main__':
import doctest
doctest.testmod()
print(part1(open('day10.txt').read())) # 339411
print(part2(open('day10.txt').read())) # 2289754624