-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
231 lines (173 loc) · 6.97 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from __future__ import annotations
from dataclasses import dataclass
from typing import Optional
from aocutils.aoc import Exercise
from aocutils.file import get_input_data_filepath
t_PAREN_OPEN = '('
t_PAREN_CLOSE = ')'
t_SQRB_OPEN = '['
t_SQRB_CLOSE = ']'
t_CRLY_OPEN = '{'
t_CRLY_CLOSE = '}'
t_LT_OPEN = '<'
t_GT_CLOSE = '>'
VALID_TOKENS = (
t_PAREN_OPEN,
t_PAREN_CLOSE,
t_SQRB_OPEN,
t_SQRB_CLOSE,
t_CRLY_OPEN,
t_CRLY_CLOSE,
t_LT_OPEN,
t_GT_CLOSE,
)
class InvalidTokenError(Exception):
def __init__(self, token: str, position: int = None) -> None:
self.token = token
message = f'Invalid token: {token}'
if position is not None:
self.position = position
message += f' at position {str(position)}'
super().__init__(message)
class InvalidClosingTokenError(Exception):
def __init__(self, chunk: NavSubsystemParser.Chunk, token: str, position: int = None) -> None:
self.chunk = chunk
self.token = token
self.expected_token = NavSubsystemParser.CLOSING_TOKENS[chunk.opening_token]
message = f'Invalid closing token {token}'
if position is not None:
self.position = position
message += f' at position {position}'
message += f'. Expected token: {self.expected_token}'
super(InvalidClosingTokenError, self).__init__(message)
class ChunkAlreadyClosedError(Exception):
pass
class NavSubsystemParser:
OPENING_TOKENS = {t_PAREN_OPEN, t_SQRB_OPEN, t_CRLY_OPEN, t_LT_OPEN}
CLOSING_TOKENS = {
t_PAREN_OPEN: t_PAREN_CLOSE,
t_SQRB_OPEN: t_SQRB_CLOSE,
t_CRLY_OPEN: t_CRLY_CLOSE,
t_LT_OPEN: t_GT_CLOSE
}
class Chunk:
def __init__(self, opening_token: str) -> None:
if opening_token not in NavSubsystemParser.OPENING_TOKENS:
raise InvalidTokenError(opening_token)
self.opening_token = opening_token
self.closing_token: Optional[NavSubsystemParser.Chunk] = None
self.parent: Optional[NavSubsystemParser.Chunk] = None
self.children: list[NavSubsystemParser.Chunk] = []
def __repr__(self) -> str:
string_repr = self.opening_token
for child in self.children:
string_repr += str(child)
if self.is_closed():
string_repr += self.closing_token
return string_repr
def close(self, closing_token: str):
if NavSubsystemParser.CLOSING_TOKENS[self.opening_token] != closing_token:
raise InvalidClosingTokenError(self, closing_token)
if self.closing_token is not None:
raise ChunkAlreadyClosedError
self.closing_token = closing_token
def is_closed(self):
return self.closing_token is not None
def add_child(self, child: NavSubsystemParser.Chunk):
if self.is_closed():
raise ChunkAlreadyClosedError
child.parent = self
self.children.append(child)
@dataclass(repr=False)
class ParsedLine:
def __init__(self, chunks: list[NavSubsystemParser.Chunk]) -> None:
self.chunks = chunks
def __repr__(self) -> str:
return ''.join([str(chunk) for chunk in self.chunks])
@classmethod
def parse_line(cls, line: str) -> NavSubsystemParser.ParsedLine:
chunks: list[cls.Chunk] = []
current_chunk: Optional[cls.Chunk] = None
for pos, char in enumerate(line):
if char not in VALID_TOKENS:
continue
if current_chunk is None:
current_chunk = cls.Chunk(char)
chunks.append(current_chunk)
continue
if char in cls.OPENING_TOKENS:
new_chunk = cls.Chunk(char)
current_chunk.add_child(new_chunk)
current_chunk = new_chunk
if char in list(cls.CLOSING_TOKENS.values()):
try:
current_chunk.close(char)
except InvalidClosingTokenError as chunk_error:
raise InvalidClosingTokenError(
chunk=chunk_error.chunk,
token=chunk_error.token,
position=pos
) from chunk_error
current_chunk = current_chunk.parent
return NavSubsystemParser.ParsedLine(chunks)
class LineCompletion:
def __init__(self, input_chunks: list[NavSubsystemParser.Chunk]) -> None:
self.input_chunks = input_chunks
def complete(self):
completion = ''
for chunk in reversed(self.input_chunks):
completion += self._complete_chunk(chunk)
return completion
def _complete_chunk(self, chunk: NavSubsystemParser.Chunk) -> str:
completion = ''
if chunk.is_closed():
return completion
for child in reversed(chunk.children):
completion += self._complete_chunk(child)
completion += NavSubsystemParser.CLOSING_TOKENS[chunk.opening_token]
return completion
class Day10(Exercise):
PARSING_SCORING_TABLE = {
t_PAREN_CLOSE: 3,
t_SQRB_CLOSE: 57,
t_CRLY_CLOSE: 1197,
t_GT_CLOSE: 25137
}
COMPLETION_SCORING_TABLE = ')]}>'
def part_one(self) -> int:
score = 0
for line in self.input_data:
try:
NavSubsystemParser.parse_line(line.strip())
except InvalidClosingTokenError as closing_error:
if closing_error.token in set(self.PARSING_SCORING_TABLE.keys()):
score += self.PARSING_SCORING_TABLE[closing_error.token]
else:
print(f'Unknown invalid closing token: {closing_error.token}')
return score
def part_two(self) -> int:
incomplete_lines = self.only_incomplete_lines()
completions = [LineCompletion(line.chunks).complete() for line in incomplete_lines]
completions_scores = sorted([self.complete_score(completion) for completion in completions])
return completions_scores[len(completions_scores) // 2]
def only_incomplete_lines(self) -> list[NavSubsystemParser.ParsedLine]:
incomplete_lines: list[NavSubsystemParser.ParsedLine] = []
for line in self.input_data:
try:
parsed_line = NavSubsystemParser.parse_line(line.strip())
except InvalidClosingTokenError:
continue
else:
incomplete_lines.append(parsed_line)
return incomplete_lines
def complete_score(self, completion: str) -> int:
line_score = 0
for char in completion:
line_score *= 5
line_score += 1 + self.COMPLETION_SCORING_TABLE.index(char)
return line_score
if __name__ == '__main__':
inputfile_path = get_input_data_filepath(__file__)
with open(inputfile_path) as input_file:
exercise = Day10(input_file.readlines())
exercise.solve_all()