forked from hugseverycat/AOC2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
73 lines (61 loc) · 2.32 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
from statistics import median
filename = 'puzzle_input/day10.txt'
#filename = 'puzzle_input/test_input.txt'
my_input = []
with open(filename) as file:
for line in file:
my_input.append(line.strip())
corruption_score = 0
incomplete_scores = []
bracket_pairs = [('[', ']'), ('(', ')'), ('<', '>'), ('{', '}')]
for this_line in my_input:
brackets = []
corrupted = False
for c in this_line:
if c in ('[', '(', '{', '<'):
# If it is an opening bracket, add it to the list
brackets.append(c)
else:
# If it is a closing bracket, take the most recent
# opening bracket and see if it pairs with the closing
# bracket
compare_c = brackets.pop()
# If the brackets aren't in the list of valid pairs, it's corrupt
if (compare_c, c) not in bracket_pairs:
corrupted = True
break
if corrupted:
if c == ')':
corruption_score += 3
elif c == ']':
corruption_score += 57
elif c == '}':
corruption_score += 1197
elif c == '>':
corruption_score += 25137
elif not corrupted: # This line is incomplete
score = 0
while True:
try:
# Since we only ever added opening brackets to the list
# and we removed opening brackets that have a valid pair
# all we are left with is opening brackets that need a closer.
# So we'll go through and evaluate their scores, starting with
# the most recent.
c = brackets.pop()
if c == '(':
score = score * 5 + 1
elif c == '[':
score = score * 5 + 2
elif c == '{':
score = score * 5 + 3
else:
score = score * 5 + 4
except IndexError:
# When there are no more brackets, store the score and move on
# to the next line in our input
incomplete_scores.append(score)
break
incomplete_scores.sort()
print("Part 1:", corruption_score)
print("Part 2:", incomplete_scores[median(range(0, len(incomplete_scores)))])