Skip to content

Commit

Permalink
feat: add second solution for 2024/4
Browse files Browse the repository at this point in the history
  • Loading branch information
hectcastro committed Dec 4, 2024
1 parent 089c6df commit a2d077e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 2024/4/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fileinput
from fileinput import FileInput

VALID_DIAGONAL_WORDS = {"MAS", "SAM"}


def count_x_mas(grid: list[list[str]]) -> int:
rows = len(grid)
cols = len(grid[0])
crossing_count = 0

for row in range(1, rows - 1):
for col in range(1, cols - 1):
# Get the northwest-southeast diagonal word centered at (row, col)
northwest_southeast_positions = [(row - 1, col - 1), (row, col), (row + 1, col + 1)]
northwest_southeast_word = "".join(grid[x][y] for x, y in northwest_southeast_positions)

# Get the northeast-southwest diagonal word centered at (row, col)
northeast_southwest_positions = [(row - 1, col + 1), (row, col), (row + 1, col - 1)]
northeast_southwest_word = "".join(grid[x][y] for x, y in northeast_southwest_positions)

if northwest_southeast_word in VALID_DIAGONAL_WORDS and northeast_southwest_word in VALID_DIAGONAL_WORDS:
crossing_count += 1

return crossing_count


def handler(raw_puzzle: FileInput) -> int:
grid = [list(line.strip()) for line in raw_puzzle]

return count_x_mas(grid)


if __name__ == "__main__":
print(handler(fileinput.input()))

0 comments on commit a2d077e

Please sign in to comment.