function countNeighbors(grid, x, y) { const neighborOffsets = [ [-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1] ]; return neighborOffsets.reduce((count, [dx, dy]) => { const nx = x + dx; const ny = y + dy; if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) { return count + 1; } return count; }, 0); } function step(grid) { return grid.map((row, x) => row.map((cell, y) => { const neighbors = countNeighbors(grid, x, y); return neighbors === 3 || (neighbors === 2 && cell) ? 1 : 0; }) ); } function printGrid(grid) { grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').join(''))); console.log('\n'); } function simulate(initial, steps) { let grid = initial; Array.from({ length: steps }).forEach(() => { printGrid(grid); grid = step(grid); }); } const initialBoard = [ [0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0], ]; simulate(initialBoard, 10); const rpentomino = [ [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]; // simulate(rpentomino, 10); // big glider const bigGlider = [ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ]; // simulate(bigGlider, 10); const randomBoard = Array.from({ length: 22 }, () => Array.from({ length: 22 }, () => Math.round(Math.random())) ); simulate(randomBoard, 50);