private int Step(Grid2D <int> grid) { int flashes = 0; var toFlash = new Queue <Point>(); foreach (var p in grid.CoordEnumerable()) { grid.SetValueAt(p, grid.ValueAt(p) + 1); if (grid.ValueAt(p) == 10) { toFlash.Enqueue(p); } } while (toFlash.Count > 0) { var flashPoint = toFlash.Dequeue(); foreach (var adjacent in grid.AdjacentPoints(flashPoint, Grid2D.allNeighbours)) { if (grid.ValueAt(adjacent) > 0) { grid.SetValueAt(adjacent, grid.ValueAt(adjacent) + 1); if (grid.ValueAt(adjacent) == 10) { toFlash.Enqueue(adjacent); } } } flashes++; grid.SetValueAt(flashPoint, 0); } return(flashes); }