-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.py
executable file
·153 lines (134 loc) · 3.97 KB
/
day04.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
#!/usr/bin/env python3
"""Day 4: Ceres Search."""
import pathlib
import re
def get_diagonals(matrix: list) -> list:
"""Get matrix diagonals.
>>> matrix = ['MMMSXXMASM',
... 'MSAMXMSMSA',
... 'AMXSXMAAMM',
... 'MSAMASMSMX',
... 'XMASAMXAMM',
... 'XXAMMXXAMA',
... 'SMSMSASXSS',
... 'SAXAMASAAA',
... 'MAMMMXMMMM',
... 'MXMXAXMASX']
>>> get_diagonals(matrix) # doctest: +NORMALIZE_WHITESPACE
['MSA', 'ASM', 'SAMM', 'MMMX', 'XMXSX', 'XSAMM', 'XXSAMX', 'XMASMA', 'MMXMAXS',
'SXMMAMS', 'ASMASAMS', 'MMXSXASA', 'SMASAMSAM', 'MASAMXXAM', 'MSAMMMMXAM',
'MSXMAXSAMX', 'AMSXXSAMX', 'MMASMASMS', 'MMAXAMMM', 'ASAMSAMA', 'XMASAMX',
'MMAMMXM', 'MMXSXA', 'XXSAMX', 'ASAMX', 'XMXMA', 'SAMM', 'SAMX', 'AMA', 'SAM']
"""
min_length = 3
rows = len(matrix)
cols = len(matrix[0])
diagonals = []
for i in range(rows + cols - 1):
diag_lr = []
diag_rl = []
for row in range(rows):
col_lr = i - row
col_rl = cols - 1 - col_lr
if 0 <= col_lr < cols:
diag_lr.append(matrix[row][col_lr])
if 0 <= col_rl < cols:
diag_rl.append(matrix[row][col_rl])
if len(diag_lr) >= min_length:
diagonals.append("".join(diag_lr))
if len(diag_lr) >= min_length:
diagonals.append("".join(diag_rl))
return diagonals
def get_columns(matrix: list) -> list:
"""Get matrix columns.
>>> matrix = ['MMMSXXMASM',
... 'MSAMXMSMSA',
... 'AMXSXMAAMM',
... 'MSAMASMSMX',
... 'XMASAMXAMM',
... 'XXAMMXXAMA',
... 'SMSMSASXSS',
... 'SAXAMASAAA',
... 'MAMMMXMMMM',
... 'MXMXAXMASX']
>>> get_columns(matrix) # doctest: +NORMALIZE_WHITESPACE
['MMAMXXSSMM', 'MSMSMXMAAX', 'MAXAAASXMM', 'SMSMSMMAMX', 'XXXAAMSMMA',
'XMMSMXAAXX', 'MSAMXXSSMM', 'AMASAAXAMA', 'SSMMMMSAMS', 'MAMXMASAMX']
"""
return ["".join(col) for col in zip(*matrix, strict=True)]
def part1(matrix: list) -> int:
"""Part One.
>>> matrix = ['MMMSXXMASM',
... 'MSAMXMSMSA',
... 'AMXSXMAAMM',
... 'MSAMASMSMX',
... 'XMASAMXAMM',
... 'XXAMMXXAMA',
... 'SMSMSASXSS',
... 'SAXAMASAAA',
... 'MAMMMXMMMM',
... 'MXMXAXMASX']
>>> part1(matrix)
18
"""
lines = matrix + get_columns(matrix) + get_diagonals(matrix)
lines += [line[::-1] for line in lines]
pattern = re.compile(r"XMAS")
return sum(len(pattern.findall(line)) for line in lines)
def part2(matrix: list) -> int:
"""Part Two.
>>> matrix = ['MMMSXXMASM',
... 'MSAMXMSMSA',
... 'AMXSXMAAMM',
... 'MSAMASMSMX',
... 'XMASAMXAMM',
... 'XXAMMXXAMA',
... 'SMSMSASXSS',
... 'SAXAMASAAA',
... 'MAMMMXMMMM',
... 'MXMXAXMASX']
>>> part2(matrix)
9
"""
pattern = re.compile(r"(M.S.A.M.S)|(M.M.A.S.S)")
rows = len(matrix)
cols = len(matrix[0])
count = 0
for i in range(rows - 2):
for j in range(cols - 2):
submatrix = [row[j : j + 3] for row in matrix[i : i + 3]]
substring = "".join("".join(row) for row in submatrix)
for m in (substring, substring[::-1]):
if pattern.match(m):
count += 1
return count
def load_input(text: str) -> list:
"""Load input text.
>>> text = '''MMMSXXMASM
... MSAMXMSMSA
... AMXSXMAAMM
... MSAMASMSMX
... XMASAMXAMM
... XXAMMXXAMA
... SMSMSASXSS
... SAXAMASAAA
... MAMMMXMMMM
... MXMXAXMASX'''
>>> load_input(text) # doctest: +NORMALIZE_WHITESPACE
['MMMSXXMASM',
'MSAMXMSMSA',
'AMXSXMAAMM',
'MSAMASMSMX',
'XMASAMXAMM',
'XXAMMXXAMA',
'SMSMSASXSS',
'SAXAMASAAA',
'MAMMMXMMMM',
'MXMXAXMASX']
"""
return text.splitlines()
if __name__ == "__main__":
input_text = (pathlib.Path(__file__).parent / "input04").read_text().strip()
values = load_input(input_text.strip())
print(f"Part One: {part1(values)}")
print(f"Part Two: {part2(values)}")