Published
Edited
Aug 29, 2018
2 stars
Insert cell
Insert cell
Insert cell
rule = 18 // Change this to any number between 0 and 255
Insert cell
ncolumns = 80
Insert cell
display = {
let map = starting_map;
let string = print(map);
while (true) yield Promises.delay(1000, string += print(map = evolve(map, rule)));
}
Insert cell
function evolve(arr, rules) {
let ruleSet = [];
ruleSet = zeroFill(8, rules.toString(2))
.split('')
.reverse();
ruleSet.forEach((item, i) => {
ruleSet[i] = parseInt(item, 10);
});
const oldArr = [];
arr.forEach((item) => {
oldArr.push(item);
});
oldArr.unshift(0);
oldArr.push(0);

const newArr = [];
oldArr.forEach((item, i) => {
if (i > 0 && i < oldArr.length - 1) {
let str = '';
str = str + oldArr[i - 1] + item + oldArr[i + 1];
for (let x = 7; x > -1; x -= 1) {
if (str === zeroFill(3, x.toString(2))) {
newArr.push(ruleSet[x]);
}
}
}
});
return newArr;
}
Insert cell
function print(map) {
let str = '';
map.forEach((item) => {
if (item === 1) {
str += '█';
} else {
str += ' ';
}
});
return str + '\n';
}
Insert cell
starting_map = {
const map = new Array(ncolumns).fill(0);
map[Math.floor(map.length / 2)] = 1;
return map;
}
Insert cell
function zeroFill(n, s) {
let zeros = '';
for (let i = 0; i < n - s.length; i += 1) {
zeros += '0';
}
const str = zeros + s;
return str;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more