import re text: str with open("input.txt") as f: text = f.read() lines: list[str] = text.splitlines() def search(values: list[list[str]], word: str, coord_x: int, coord_y: int, inc_x: int, inc_y: int): array_length = len(values) line_length = len(values[0]) for i in range(len(word)): x: int = coord_x + i * inc_x y: int = coord_y + i * inc_y if (x >= line_length or x < 0) or (y >= array_length or y < 0) or (values[y][x] != word[i]): return 0 return 1 total: int = 0 directions = [(-1, -1), (0, -1), (+1, -1), (+1, 0), (+1, +1), (0, +1), (-1, +1), (-1, 0)] for y, line in enumerate(lines): for x, c in enumerate(line): if (c == 'X'): for inc_x, inc_y in directions: total += search(lines, "XMAS", x, y, inc_x, inc_y) print(total)