""" day 4 part 1 """ import fileinput def match_word(grid, word, x, y, dx, dy): for letter in word: if y < 0 or y >= len(grid) or x < 0 or x >= len(grid[y]): return False if grid[y][x] != letter: return False x += dx y += dy return True def count_word(grid, word): count = 0 for y in range(len(grid)): for x in range(len(grid[y])): for dx in range(-1,2): for dy in range(-1,2): if (dx or dy) and match_word(grid, word, x, y, dx, dy): count += 1 return count grid = [line for line in fileinput.input()] print(count_word(grid, 'XMAS'))