Published
Edited
Dec 17, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
function* neighbors([x, y, z, w]) {
for (let dx of [-1, 0, 1])
for (let dy of [-1, 0, 1])
for (let dz of [-1, 0, 1])
for (let dw of [-1, 0, 1])
if (dx !== 0 || dy !== 0 || dz !== 0 || dw !== 0)
yield [x + dx, y + dy, z + dz, w + dw]
}
Insert cell
function tick(cells) {
let counts = {}
for (const cell of cells)
for (const n of neighbors(cell.split(',').map(Number)))
counts[n] = (counts[n] || 0) + 1
let next = new Set()
for (const [cell, count] of Object.entries(counts))
if (count === 3 || (count == 2 && cells.has(cell)))
next.add(cell)

return next
}
Insert cell
seed = new Set(input.trim().split('\n')
.flatMap((l, x) => l.split('').map((v, y) => v === '#' ? [x, y, 0, 0] : null))
.filter(d => d)
.map(String))
Insert cell
fps = 5
Insert cell
zoom = 10
Insert cell
alive = {
let cells = seed
for (let n = 0; n < 6; n++) {
yield Promises.tick(1000/fps, cells = tick(cells))
}
}
Insert cell
// alive = {
// let cells = this || seed
// while (true) {
// if (fps === 0)
// return yield cells
// yield Promises.tick(1000/fps, cells = tick(cells))
// }
// }
Insert cell
Insert cell
d3 = require('d3@6')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more