| Problem statement | Source code | Tags: BFS/DFS
We just need to take care to not double-count flashes in a single step. For each new flash, it should affect its neighbors, which may cause more flashes. This is still a flood fill kind of problem, so I still use DFS. I have a stack of flashers to process. Each time a new octopus flashes, we add it to the stack. When processing a flasher, we increase the energy level of its neighbors, and new flashers are added to the stack. Continue until there are no more flashers to process.
Given the step function from part 1, we can just keep stepping until all octopuses flash simultaneously, which is when all energy levels are 0.