Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
predefined list of surrounded coordinates solution in Clear category for [old] Moore Neighbourhood by oduvan
"use strict";
var AROUND = [];
for(let i=-1; i<=1; i++){
for(let j=-1; j<=1; j++){
if (i == 0 && j == 0) continue;
AROUND.push([i, j])
}
}
function countNeighbours(data, row, col) {
return AROUND.reduce((a,b) => {
let bx = row + b[0], by = col + b[1];
if (data[bx] && data[bx][by])
return a + data[bx][by]
else return a
}, 0)
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(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))
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!");
}
April 4, 2018
Comments: