Skip to content

Instantly share code, notes, and snippets.

@Gankra
Created December 6, 2012 17:49
Show Gist options
  • Save Gankra/4226483 to your computer and use it in GitHub Desktop.
Save Gankra/4226483 to your computer and use it in GitHub Desktop.
<script>
/*
var grid = [
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]
];
*/
var grid = [
[3,1,2,3,1,3],
[2,1,3,4,1,2],
[4,1,4,2,1,4],
[3,2,5,3,1,3],
[1,4,1,5,4,2],
[2,3,1,2,3,1]
];
console.log(sum(grid),verify(grid));
function verify(grid){
var correct = true;
for(var i=0; i<grid.length; i++){
var curRow = grid[i];
for(var j=0; j<curRow.length; j++){
if(!satisfied(grid,i,j)){
console.log("Error at ("+i+","+j+")");
correct = false;
}
}
}
return correct;
}
function satisfied(grid, row, col){
var neighbours = [];
var val = grid[row][col];
neighbours[getVal(grid, row-1, col)] = true;
neighbours[getVal(grid, row+1, col)] = true;
neighbours[getVal(grid, row, col-1)] = true;
neighbours[getVal(grid, row, col+1)] = true;
for(var i=1;i<val;i++){
if(!neighbours[i]){
return false;
}
}
return true;
}
function getVal(grid, row, col){
var row = grid[row]
if(row){
var val = row[col];
if(val){
return val;
}
}
return 0;
}
function sum(grid){
var sum = 0;
for(var i=0; i<grid.length; i++){
var curRow = grid[i];
for(var j=0; j<curRow.length; j++){
sum+=curRow[j];
}
}
return sum;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment