#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def count_vertical(data:list[list[str]], pattern:str) -> int:
    result = 0
    for i in range(len(data)):
        for j in range(len(data[0])):
            try:
                if (data[i][j] == pattern[0] and data[i+1][j] == pattern[1] and data[i+2][j] == pattern[2]
                    and data[i+3][j] == pattern[3]):
                        result += 1
            except IndexError:
                pass
    return result

def count_diagonal(data:list[list[str]], pattern:str) -> int:
    result = 0
    for i in range(len(data)):
        #print(data[i])
        for j in range(len(data[0])):
            try:
                if (data[i][j] == pattern[0] and data[i+1][j+1] == pattern[1]
                    and data[i+2][j+2] == pattern[2] and data[i+3][j+3] == pattern[3]):
                        result += 1
            except IndexError:
                pass
            try:
                if (j > 2 and data[i][j] == pattern[0] and data[i+1][j-1] == pattern[1]
                    and data[i+2][j-2] == pattern[2] and data[i+3][j-3] == pattern[3]):
                        result += 1
            except IndexError:
                pass
    return result


if __name__ == "__main__":
    solution = 0
    field=[]
    input_file = open("input.txt", "r")
    for line in input_file:
        line = line.strip()
        if line == "":
            continue
        solution += line.count('XMAS')
        solution += line.count('SAMX')
        temp = []
        for b in line:
            temp.append(b)
        field.append(temp)
    input_file.close()

    solution += count_vertical(field, 'XMAS')
    solution += count_vertical(field, 'SAMX')
    solution += count_diagonal(field, 'XMAS')
    solution += count_diagonal(field, 'SAMX')

    #print(field)
    print(f'Solution:{solution}')