next = (octopuses) => {
let current = [...octopuses.map(xs => [...xs])]
const w = current[0].length
const h = current.length
let flashes = []
let f_ = []
let f2 = []
let flash = (octopus) => {
const x = octopus.x
const y = octopus.y
flashes.push({x,y})
f_ = f_.filter(f => !(f.x == x && f.y == y))
for (let x_ = Math.max(0, x-1); x_ <= Math.min(w-1, x+1); x_++){
for (let y_ = Math.max(0, y-1); y_ <= Math.min(h-1, y+1); y_++){
current[y_][x_] += 1
if (current[y_][x_] > 9 && flashes.filter(f => f.x == x_ && f.y == y_).length == 0) f_.push({x: x_, y: y_})
}
}
}
for (let x = 0; x < w; x++){
for (let y = 0; y < h; y++){
current[y][x] += 1
if (current[y][x] > 9) f_.push({x,y})
}
}
while(f_.length > 0) flash(f_.shift())
flashes.forEach(o => current[o.y][o.x] = 0)
return current
}