-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
52 lines (37 loc) · 1.35 KB
/
part_two.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
from typing import override
from infrastructure.solutions.base import Solution
class Year2024Day4Part2Solution(Solution):
STRAIGHT_TARGET = 'MAS'
REVERSED_TARGET = STRAIGHT_TARGET[::-1]
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[str]]:
matrix = []
for line in text_input.split('\n'):
if line:
matrix.append(line)
return {'matrix': matrix}
@classmethod
@override
def solve(cls, matrix: list[str]) -> int:
"""
Time: O(n*m*k)
Space: O(k)
Where n - number of rows in matrix
m - number of columns in matrix
k - target string length
"""
n, m = len(matrix), len(matrix[0])
matches = 0
for i in range(1, n - 1):
for j in range(1, m - 1):
main_diag = matrix[i - 1][j - 1] + matrix[i][j] + matrix[i + 1][j + 1]
side_diag = matrix[i - 1][j + 1] + matrix[i][j] + matrix[i + 1][j - 1]
if main_diag not in (cls.STRAIGHT_TARGET, cls.REVERSED_TARGET):
continue
if side_diag not in (cls.STRAIGHT_TARGET, cls.REVERSED_TARGET):
continue
matches += 1
return matches
if __name__ == '__main__':
print(Year2024Day4Part2Solution.main())