#!/usr/bin/env python # 2024 - Advent Of Code 4 - part 2 # file = 'input_example.txt' file = 'input.txt' # pylint: disable=consider-using-with input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] MAX_X = len(input_lines[0]) MAX_Y = len(input_lines) # for line in input_lines: # print(f'{line}') def find_xmas(grid, start_pos): x = start_pos[0] y = start_pos[1] if grid[y - 1][x - 1] == 'M' and grid[y - 1][x + 1] == 'M': if grid[y + 1][x - 1] == 'S' and grid[y + 1][x + 1] == 'S': return True if grid[y - 1][x - 1] == 'M' and grid[y + 1][x - 1] == 'M': if grid[y - 1][x + 1] == 'S' and grid[y + 1][x + 1] == 'S': return True if grid[y - 1][x + 1] == 'M' and grid[y + 1][x + 1] == 'M': if grid[y - 1][x - 1] == 'S' and grid[y + 1][x - 1] == 'S': return True if grid[y + 1][x - 1] == 'M' and grid[y + 1][x + 1] == 'M': if grid[y - 1][x - 1] == 'S' and grid[y - 1][x + 1] == 'S': return True return False accum = 0 for pos_y in range(1, MAX_Y - 1): for pos_x in range(1, MAX_X - 1): if input_lines[pos_y][pos_x] == 'A': if find_xmas(input_lines, (pos_x, pos_y)): accum += 1 print(f'{accum=}')