Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PoYRvxv
function pos(x, y, width) {
return (x * width) + y;
}
// Initial Pattern is the seed
const seed = (() => {
let arr = new Array(3600);
arr.fill(false);
arr[pos(0, 1, 60)] = true;
arr[pos(1, 2, 60)] = true;
arr[pos(2, 0, 60)] = true;
arr[pos(2, 1, 60)] = true;
arr[pos(2, 2, 60)] = true;
return arr;
})();
function printCell(cell) {
return cell ? '#' : ' ';
}
function print(state) {
const width = Math.sqrt(state.length);
for (let x = 0; x < width; x = x + 1) {
console.log(' ' + (new Array(width)).fill('-').join(''));
let row = '|';
for (let y = 0; y < width; y = y +1) {
row = row + printCell(state[pos(x, y, width)]);
}
console.log(row + '|');
}
console.log(' ' + (new Array(width)).fill('-').join(''));
}
// console.log(pos(0, 0, 10));
// console.log(pos(1, 0, 10));
// console.log(pos(1, 3, 10));
// console.log(pos(3, 2, 10));
// console.log(seed);
// print(seed);
const topLeft = [-1, -1];
const top = [-1, 0];
const topBottom = [-1, 1];
const left = [0, -1];
const right = [0, 1];
const bottomLeft = [1, -1];
const bottom = [1, 0];
const botttomRight = [1, 1];
console.log(pos(...topLeft, 10));
function printAndReturn(val) {
return val;
}
function numNeighbors(idx, state, width) {
return [
idx + pos(...topLeft, width),
idx + pos(...top, width),
idx + pos(...topBottom, width),
idx + pos(...left, width),
idx + pos(...right, width),
idx + pos(...bottomLeft, width),
idx + pos(...bottom, width),
idx + pos(...botttomRight, width)
].filter(i => i >= 0)
.map(i => state[i])
.filter(v => v === true).length;
}
function tick(state) {
/*
Any live cell with fewer than two live neighbours dies, as if by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
const width = Math.sqrt(state.length);
return state.map((cell, idx, s) => {
let flipState = false;
let numN = numNeighbors(idx, s, width);
if (cell === true) {
flipState = numN < 2 || numN > 3;
} else {
flipState = numN === 3
}
return flipState ? !cell : cell;
});
}
console.log(numNeighbors(pos(1, 1, 10), seed, 10) === 2);
console.log(numNeighbors(pos(2, 1, 10), seed, 10) === 3);
console.log(numNeighbors(pos(3, 1, 10), seed, 10) === 2);
const state2 = tick(seed);
const state3 = tick(tick(seed));
const state4 = tick(tick(tick(seed)));
const state5 = tick(tick(tick(tick(seed))));
const state6 = tick(tick(tick(tick(tick(seed)))));
function tickNumTimes(times, startingState) {
let newState = startingState;
for (let x = 0; x < times; x = x + 1) {
newState = tick(newState);
}
return newState;
}
let arr = new Array(20)
let states = arr.fill().map((v, i) => {
return tickNumTimes(i, seed);
});
let idx = 0;
console.clear();
setInterval(() => {
idx = idx >= (states.length - 1) ? 0 : idx + 1;
console.clear();
console.log(`State ${idx} / ${states.length}`);
print(states[idx]);
}, 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.