From 9193b35822950f0ae14dc7f990ad5922cc97ce60 Mon Sep 17 00:00:00 2001 From: He4eT Date: Mon, 23 Sep 2019 17:52:59 +0500 Subject: [PATCH] Life: implementation --- src/life.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/life.js diff --git a/src/life.js b/src/life.js new file mode 100644 index 0000000..8a5381f --- /dev/null +++ b/src/life.js @@ -0,0 +1,46 @@ +const countNeighbours = grid => ([y, x]) => { + const sum = (a, b) => a + b + + const area = (grid, y, x) => { + const [yf, xf] = [y, x].map(c => Math.max(0, c - 1)) + const [yt, xt] = [y, x].map(c => c + 2) + return grid + .slice(yf, yt) + .map(row => row.slice(xf, xt)) + } + + const areaSum = + area(grid, y, x) + .map(row => + row.reduce(sum, 0)) + .reduce(sum, 0) + + return [areaSum - grid[y][x], grid[y][x]] +} + +/** + * Any live cell with fewer than two live neighbours dies, as if by underpopulation. + * Any live cell with more than three live neighbours dies, as if by overpopulation. + * Any live cell with two or three live neighbours lives on to the next generation. + * Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. + */ +const deadOrAlive = ([neighbours, cell]) => { + if (cell && neighbours < 2) return 0 + if (cell && neighbours > 3) return 0 + if (cell && [2, 3].includes(neighbours)) return 1 + if (!cell && neighbours === 3) return 1 + else return 0 +} + +const lifeStep = grid => + grid.map((row, y) => + row + .map((_, x) => [y, x]) + .map(countNeighbours(grid)) + .map(deadOrAlive)) + +module.exports = { + countNeighbours, + lifeStep, + deadOrAlive +}