function countNeighbors (idx, grid) {
let counts = { '#': 0, '.': 0, '|': 0 };
let neighborhood = [];
let col = idx % SIZE;
let row = (idx - col) / SIZE;
let minRow = Math.max(0, row - 1);
let maxRow = Math.min(SIZE, row + 2);
let minCol = Math.max(0, col - 1);
let maxCol = Math.min(SIZE, col + 2);
for (let i = minRow; i < maxRow; i++) {
for (let j = minCol; j < maxCol; j++) {
let nidx = j + i * SIZE;
counts[grid[nidx]]++;
neighborhood.push(nidx);
}
}
return [counts, neighborhood];
}