text: str with open("input.txt") as f: text = f.read() total: int = 0 def eval(lines: list[str], x: int, y: int): start_x = max(x - 1, 0) stop_x = min(len(lines[0]), x + 2) start_y = max(y - 1, 0) stop_y = min(len(lines[0]), y + 2) count: int = 0 for line in lines[start_y:stop_y]: count += line[start_x:stop_x].count("@") return ((count - 1) < 4) lines = [[c for c in line] for line in text.splitlines()] bozo = 1 while bozo: bozo = 0 for y, line in enumerate(lines): for x, c in enumerate(line): if c == "@": if (eval(lines, x, y)): total += 1 line[x] = "x" bozo = 1 print(total)