Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Life Counter by Moff
class InfiniteLife {
constructor() {
this.area = new Set();
this.delta = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
}
neighbours(key) {
let cell = JSON.parse(key);
return this.delta.map(([i, j]) => JSON.stringify([cell[0] + i, cell[1] + j]));
}
neighboursDict() {
let result = {};
for (let key of this.area) {
for (let c of this.neighbours(key))
result[c] = result[c] + 1 || 1;
}
return result;
}
iterate() {
let newArea = new Set();
let dict = this.neighboursDict();
for (let key in dict) {
let n = dict[key];
if (n === 3 || (n === 2 && this.area.has(key)))
newArea.add(key);
}
this.area = newArea;
}
}
function lifeCounter(area, steps) {
let game = new InfiniteLife();
area.forEach((row, i) => {
row.forEach((v, j) => {
if (v)
game.area.add(JSON.stringify([i, j]));
});
});
for (let i = 0; i < steps; i++)
game.iterate();
return game.area.size;
}
Aug. 4, 2017