20/32
Part 5 of 7 · RoundsLearn and do

A fire that goes out this minute still lights its neighbors this minute

So far a fire burned forever. Now it burns for one minute.

A fire burns for one minute, then turns to ash, A
row
minute 0F..minute 1AF.minute 2AAFminute 3AAA

At minute 1, (0,0) goes out, and in the same minute it lights (0,1).

Real rules change more than one thing per round: a fire that burns for one minute and then turns to ash, A, or an infected cell that recovers. Reading the old grid handles both in the same pass. A fire that goes out this minute was still burning at the start of the minute, so it still lights its neighbors this minute. Write the order of events down before you code it, because that's where a wrong order or an in-place write hides.

When a fire burns for bb minutes instead of one, you know the minute it goes out the moment it catches: that minute plus bb. Keep a schedule, a defaultdict(list) from a minute to the cells that go out then. Add a cell when it catches (ends[minute + b].append(cell)). At each minute, apply ends.pop(minute, []) together with that minute's new fires. Here is F.. with b=3b = 3:

A fire that burns for b = 3 minutes
row
minute 0F..minute 1FF.minute 2FFFminute 3AFFminute 4AAFminute 5AAA

(0,0), (0,1) and (0,2) catch at minutes 0, 1 and 2, and go out at 3, 4 and 5.

In the editor, write the loop in burn_once(grid, k): one minute of a forest where fires burn out. Every fire F turns to ash A, and every tree with at least k burning neighbors at the start of the minute catches.