-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (57 loc) · 2.06 KB
/
main.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
class Day10:
def __init__(self):
self.puzzle_input = self.get_puzzle_input()
self.lines_corrupted = []
self.lines_incomplete = []
self.parse_lines()
def part_one(self) -> int:
return self.get_corrupted_score()
def part_two(self) -> int:
return self.get_incomplete_score()
def get_corrupted_score(self) -> int:
point_system = {')': 3, ']': 57, '}': 1197, '>': 25137}
return sum([point_system[x] for x in self.lines_corrupted])
def get_incomplete_score(self) -> int:
point_system = {'(': 1, '[': 2, '{': 3, '<': 4}
def calc_score(lst):
lst.reverse()
score = 0
for char in lst:
score = (score * 5) + point_system[char]
return score
scores = [calc_score(lst) for lst in self.lines_incomplete]
scores.sort()
return scores[len(scores) // 2]
def parse_lines(self):
data = self.puzzle_input.copy()
for line in data:
stack = []
is_incomplete = True
for char in line:
if self.is_opening_bracket(char):
stack.append(char)
else:
if stack[-1] == self.get_closing_bracket(char):
stack.pop()
else:
is_incomplete = False
self.lines_corrupted.append(char)
break
if is_incomplete:
self.lines_incomplete.append(stack)
@staticmethod
def get_closing_bracket(char):
return {')': '(', ']': '[', '>': '<', '}': '{'}[char]
@staticmethod
def is_opening_bracket(char):
return True if char in ['(', '[', '<', '{'] else False
@staticmethod
def get_puzzle_input() -> list:
lst = []
with open("input.txt") as file:
for line in file:
lst.append(line.rstrip())
return lst
day = Day10()
print(f'Result part 1: {day.part_one()}')
print(f'Result part 2: {day.part_two()}')