with open('puzzle.txt', 'r') as f: lines = f.read().splitlines() xmas = 0 for y in range(len(lines)): for x in range(len(lines[y])): if lines[y][x] == 'A': extra_check = [] if 0 < x < len(lines[y]) - 1 and 0 < y < len(lines) - 1: if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'S' and lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'S'): xmas += 1 if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'M' and lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'S'): xmas += 1 if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'M' and lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'M'): xmas += 1 if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'S' and lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'M'): xmas += 1 print(xmas)