import re text: str with open("input.txt") as f: text = f.read() lines: list[str] = text.splitlines() total: int = 0 for line1, line2, line3 in zip(lines[0:-2], lines[1:-1], lines[2:]): for i in range(0, len(line1) - 2): if (line2[i + 1] == "A"): total += (line1[i] == "M" and line3[i] == "M" and line1[i + 2] == "S" and line3[i + 2] == "S") total += (line1[i] == "M" and line3[i] == "S" and line1[i + 2] == "M" and line3[i + 2] == "S") total += (line1[i] == "S" and line3[i] == "S" and line1[i + 2] == "M" and line3[i + 2] == "M") total += (line1[i] == "S" and line3[i] == "M" and line1[i + 2] == "S" and line3[i + 2] == "M") print(total)