from lib import * row = open(0).read().strip() def is_trap(s): assert len(s) == 3 match s: case "^^.": return "^" case ".^^": return "^" case "^..": return "^" case "..^": return "^" case _: return "." # 400_000 is still easily bruteforcible. If it was a much larget number, we # would have to cache the rows till we get a repeated row. We would then # memorize the number of safe tiles from repeated row to repeated row and jump # forward by multiples of that amount to reach much higher numbers. r = 0 for _ in range(400_000): r += row.count(".") nrow = is_trap("." + row[:2]) for i in range(1, len(row) - 1): nrow += is_trap(row[i-1:i+2]) nrow += is_trap(row[-2:] + ".") row = nrow print(r)