Published
Edited
Mar 30, 2021
Importers
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
spacetime = generateSpacetime({
timeSize: 20,
spaceSize: 10,
table,
stateCount
})
Insert cell
Insert cell
function generateSpacetime(args) {
const {
timeSize,
spaceSize,
table,
stateCount,
startFill,
borderFill,
random
} = {
startFill: "random",
borderFill: "zeros",
random: Math.random,
...args
};
if (startFill !== "random") {
throw new Error(`startFill: ${startFill} is not supported`);
}
if (borderFill !== "zeros") {
throw new Error(`borderFill: ${borderFill} is not supported`);
}
const spacetime = Array.from({ length: timeSize }, () =>
Array.from({ length: spaceSize })
);
fillSpacetime(spacetime, table, stateCount);
return spacetime;
}
Insert cell
Insert cell
function fillSpace(spacetime, t, table, stateCount) {
const nr = 1;
const prevPrevSpace = spacetime[t - 2];
const prevSpace = spacetime[t - 1];
const space = spacetime[t];
for (let x = nr; x < space.length - nr; x++) {
const cs = getFullCombinedState(
prevSpace[x - 1],
prevSpace[x],
prevSpace[x + 1],
prevPrevSpace[x],
stateCount
);
space[x] = table[cs];
}
}
Insert cell
function getFullCombinedState(n1, c, n2, pc, stateCount) {
var combinedState = 0;
combinedState = combinedState * stateCount + pc;
combinedState = combinedState * stateCount + n2;
combinedState = combinedState * stateCount + c;
combinedState = combinedState * stateCount + n1;
return combinedState;
}
Insert cell
function fillSpacetime(spacetime, table, stateCount) {
const nr = 1;
for (let t = 0; t < 2; t++) {
const space = spacetime[t];
for (let x = 0; x < space.length; x++) {
space[x] = Math.floor(Math.random() * stateCount);
}
}
for (let t = 2; t < spacetime.length; t++) {
fillSpace(spacetime, t, table, stateCount);
const space = spacetime[t];
for (let x = 0; x < nr; x++) {
space[x] = 0;
space[space.length - 1 - x] = 0;
}
}
}
Insert cell
Insert cell
function getDigits(n, base) {
return [...n.toString(base)].map(Number).reverse();
}
Insert cell
function getNumberFromDigits(digits, base) {
base = BigInt(base);
let n = 0n;
for (let i = digits.length - 1; i >= 0; i--) {
let d = BigInt(digits[i]);
n *= base;
n += d;
}
return n;
}
Insert cell
getDigits(166148131829022414913355465289613568111n, 3)
Insert cell
166148131829022414913355465289613568111n ===
getNumberFromDigits(getDigits(166148131829022414913355465289613568111n, 3), 3)
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