import re lines = [l for l in open("input.txt", 'r').read().splitlines()] linesVert, linesDiag1, linesDiag2 = [], [], [] count, count2, width = 0, 0, len(lines[0]) for i in range(width): string, string2, string3 = "", "", "" for j in range(len(lines)): string += lines[j][i] string2 += lines[j][(i+j)%width] string3 += lines[width-j-1][(i+j)%width] linesDiag1.append(string2[:width - i]) linesDiag1.append(string2[width - i:]) linesDiag2.append(string3[:width - i]) linesDiag2.append(string3[width - i:]) linesVert.append(string) for l in lines + linesVert + linesDiag1 + linesDiag2: count += len(re.findall(r"(?=(XMAS|SAMX))", l)) for i in range(width-2): for j in range(len(lines)-2): diag1 = lines[j][i] + lines[j+1][i+1] + lines[j+2][i+2] diag2 = lines[j+2][i] + lines[j+1][i+1] + lines[j][i+2] if re.search(r"MAS|SAM", diag1) != None and re.search(r"MAS|SAM", diag2) != None: count2 += 1 print(count) print(count2)