-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
49 lines (43 loc) · 1.62 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
from common.day import Day
class Day10(Day):
pairs = {'(': ')', '{': '}', '[': ']', '<': '>'}
def part1(self) -> int:
coef = {')': 3, ']': 57, '}': 1197, '>': 25137}
table = self.read_input_lines()
result = 0
for row in table:
closing_stack = []
for symbol in row:
if symbol in self.pairs:
closing_stack.append(self.pairs[symbol])
elif symbol != closing_stack.pop():
result += coef[symbol]
break
return result
def part2(self) -> int:
coef = {")": 1, "]": 2, "}": 3, ">": 4}
table = self.read_input_lines()
corrupted_symbols = []
remaining_symbols = []
for row in table:
closing_stack = []
is_corrupted_row = False
for symbol in row:
if symbol in self.pairs:
closing_stack.append(self.pairs[symbol])
elif symbol != closing_stack.pop():
corrupted_symbols.append(symbol)
is_corrupted_row = True
break
if not is_corrupted_row and len(closing_stack) > 0:
remaining_symbols.append(closing_stack)
scores = []
for stack in remaining_symbols:
row_score = 0
self.logger.debug(stack)
for i, symbol in enumerate(stack):
row_score += 5 ** i * coef[symbol]
self.logger.debug(row_score)
scores.append(row_score)
scores = sorted(scores)
return scores[len(scores) // 2]