Simplify test input

This commit is contained in:
2024-12-04 22:08:54 -08:00
parent 80b85b459e
commit 00a3bdd07b
6 changed files with 28 additions and 23 deletions

View File

@@ -1,7 +1,7 @@
from typing import Iterator
test_input_p1 = test_input_p2 = """
test_input = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
@@ -39,8 +39,7 @@ def _scan_word_grid(
yield from (row for row in char_grid)
yield from (tuple(col) for col in zip(*char_grid))
yield from (
_diagonal(char_grid, i)
for i in range(-len(char_grid) + 1, len(char_grid[0]))
_diagonal(char_grid, i) for i in range(-len(char_grid) + 1, len(char_grid[0]))
)
yield from (
_diagonal(tuple(reversed(char_grid)), i)
@@ -57,28 +56,29 @@ def _diagonal(grid: tuple[tuple[str, ...], ...], offset=0) -> tuple[str, ...]:
def _count_xmas(line: tuple[str, ...]) -> int:
return sum(
1
for i
in range(len(line) - 3)
for i in range(len(line) - 3)
if line[i : i + 4] in (("X", "M", "A", "S"), ("S", "A", "M", "X"))
)
ThreeSquare = tuple[
tuple[str, str, str],
tuple[str, str, str],
tuple[str, str, str]
]
ThreeSquare = tuple[tuple[str, str, str], tuple[str, str, str], tuple[str, str, str]]
def _three_squares(word_grid: tuple[tuple[str, ...], ...]) -> Iterator[ThreeSquare]:
yield from (
(word_grid[i][j:j+3], word_grid[i+1][j:j+3], word_grid[i+2][j:j+3])
(
word_grid[i][j : j + 3],
word_grid[i + 1][j : j + 3],
word_grid[i + 2][j : j + 3],
)
for i in range(len(word_grid) - 2)
for j in range(len(word_grid[0]) - 2)
)
def _has_cross_mas(square: ThreeSquare):
diag_1 = (square[0][0], square[1][1], square[2][2])
diag_2 = (square[0][2], square[1][1], square[2][0])
return (
(diag_1 == ("M", "A", "S") or diag_1 == ("S", "A", "M"))
and (diag_2 == ("M", "A", "S") or diag_2 == ("S", "A", "M"))
)
return (diag_1 == ("M", "A", "S") or diag_1 == ("S", "A", "M")) and (
diag_2 == ("M", "A", "S") or diag_2 == ("S", "A", "M")
)