verbose = False def x_mas_at(M, i: int, j: int) -> bool: # NOTE: Checking i and j are not on the boundary is wise. top_left = \ M[i - 1][j - 1] == "M" and M[i + 1][j + 1] == "S" or \ M[i - 1][j - 1] == "S" and M[i + 1][j + 1] == "M" top_right = \ M[i + 1][j - 1] == "M" and M[i - 1][j + 1] == "S" or \ M[i + 1][j - 1] == "S" and M[i - 1][j + 1] == "M" return top_left and top_right if __name__ == "__main__": result = 0 with open("input", "r") as fp: # Skip last line as it is empty. full_string = fp.read().split("\n")[:-1] x_mas_count = 0 for i in range(1, len(full_string) - 1): for j in range(1, len(full_string[0]) - 1): if full_string[i][j] == "A": if x_mas_at(full_string, i, j): x_mas_count += 1 print(f"The sum of all horizontal X-MASs equals {x_mas_count}.")