-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 10.py
65 lines (62 loc) · 1.88 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
test = '''[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]'''.split('\n')
inp = open('Day 10.txt','r').read().split('\n')
def part1(inp):
opening = ['(','[','{','<']
closing = [')',']','}','>']
corrupted = 0
for i in inp:
indent = []
for s in i:
if s in opening:
indent.append(s)
else:
if closing.index(s) == opening.index(indent[-1]):
indent.pop(-1)
else:
match s:
case ')': corrupted += 3
case ']': corrupted += 57
case '}': corrupted += 1197
case '>': corrupted += 25137
break
print(corrupted)
def part2(inp):
opening = ['(','[','{','<']
closing = [')',']','}','>']
scores = []
for i in inp:
corrupted = False
indent = []
for s in i:
if s in opening:
indent.append(s)
else:
if closing.index(s) == opening.index(indent[-1]):
indent.pop(-1)
else:
corrupted = True
break
if corrupted: continue
indent.reverse()
indent = [closing[opening.index(x)] for x in indent]
score = 0
for x in indent:
score *= 5
match x:
case ')': score += 1
case ']': score += 2
case '}': score += 3
case '>': score += 4
scores.append(score)
print(sorted(scores)[int((len(scores)-1)/2)])
part1(inp)
part2(inp)