Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for [old] Moore Neighbourhood by chaowman
"use strict";
function countNeighbours(data, row, col) {
var f = (r,c) => ~~(data[r] && data[r][c]);
return f(row-1,col-1) + f(row-1,col) + f(row-1,col+1) +
f(row ,col-1) + f(row ,col+1) +
f(row+1,col-1) + f(row+1,col) + f(row+1,col+1);
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 1, 2), 3, "1st example");
assert.equal(countNeighbours([[1, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[1, 0, 0, 0, 0],
[0, 0, 1, 0, 0]], 0, 0), 1, "2nd example");
assert.equal(countNeighbours([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]], 0, 2), 3, "Dense corner");
assert.equal(countNeighbours([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]], 1, 1), 0, "Single");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Sept. 14, 2016
Comments: