advent-of-code-2024/day4.2.py

29 lines
623 B
Python

""" day 4 part 2 """
import fileinput
def match_mas(grid, x, y):
if grid[y][x] != 'A':
return False
w1 = grid[y-1][x-1] + grid[y][x] + grid[y+1][x+1]
if w1 != 'MAS' and w1 != 'SAM':
return False
w1 = grid[y+1][x-1] + grid[y][x] + grid[y-1][x+1]
if w1 != 'MAS' and w1 != 'SAM':
return False
return True
def count_mas(grid):
count = 0
for y in range(1,len(grid) - 1):
for x in range(1, len(grid[y]) - 1):
if match_mas(grid, x, y):
count += 1
return count
grid = [line for line in fileinput.input()]
print(count_mas(grid))