Skip to content

Instantly share code, notes, and snippets.

Created April 12, 2017 08:29
Show Gist options
  • Save anonymous/63db466640eee787a08437a614a191a5 to your computer and use it in GitHub Desktop.
Save anonymous/63db466640eee787a08437a614a191a5 to your computer and use it in GitHub Desktop.
6.6 Sudoku created by smillaraaq - https://repl.it/HGS2/42
var puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
[ 2,7,1, 9,6,3, 4,8,5 ],
[ 4,6,3, 5,8,1, 7,9,2 ],
[ 9,3,4, 6,1,7, 2,5,8 ],
[ 5,1,7, 2,3,8, 9,6,4 ],
[ 6,8,2, 4,5,9, 3,7,1 ],
[ 1,5,9, 8,7,4, 6,2,3 ],
[ 7,4,6, 3,2,5, 8,1,9 ],
[ 3,2,8, 1,9,6, 5,4,7 ]]; //==> passes
var p8zzle = [[ 8,8,5,7,4,2,1,3,6 ],
[ 8,7,1,9,6,3,4,8,5 ],
[ 4,6,3,5,8,1,7,9,2 ],
[ 9,3,4,6,1,7,2,5,8 ],
[ 5,1,7,2,3,8,9,6,4 ],
[ 6,8,2,4,5,9,3,7,1 ],
[ 1,5,9,8,7,4,6,2,3 ],
[ 7,4,6,3,2,5,8,1,9 ],
[ 3,2,8,1,9,6,5,4,7 ]]; //==> fails
function sudokuChecker(puzzle){
// returns true if:
// Each column must contain the numbers 1-9 (no repeats!)
// Each row must contain the numbers 1-9 (no repeats!)
// Each 3x3 subgrid must contain the numbers 1-9 (no repeats!)
//check row
for(var i=0;i<puzzle.length;i++){
var section=getRow(puzzle,i);
if(!includes1to9(section)){
console.log("Row "+i+" is broken");
return false;
}
}
//check col
for(var i=0;i<puzzle.length;i++){
var section=getColumn(puzzle,i);
if(!includes1to9(section)){
console.log("Column "+i+" is broken");
return false;
}
}
//check subgrid
for(var i=0;i<3;i++){ //3 is hardcoded
for(var j=0;j<3;j++){
var section=getSubGrid(puzzle,i,j);
if(!includes1to9(section)){
console.log("SubGrid (" +i+ ", " +j+ ") is broken");
return false;
}
}
}
return true;
}
sudokuChecker(puzzle);
function getRow(grid, rowIndex){
var currentRow=grid[rowIndex];
return currentRow;
}
// getRow(puzzle, 8); // -> [ 3,2,8,1,9,6,5,4,7 ]
function getColumn(grid,colIndex){
var currentColumn=[];
for(var i=0;i<grid.length;i++){
currentColumn.push(grid[i][colIndex]);
}
return currentColumn;
}
// getColumn(puzzle, 0); // -> [ 8,2,4,9,5,6,1,7,3 ]
function getSubGrid(grid,subgridCol,subgridRow){
var currentCube=[];
var cubeDimension=3;
var startIndexRow=subgridRow*cubeDimension;
var startIndexCol=subgridCol*cubeDimension;
var endIndexRow=startIndexRow+cubeDimension;
var endIndexCol=startIndexCol+cubeDimension;
for(var i=startIndexRow;i<endIndexRow;i++){
for(var j=startIndexCol;j<endIndexCol;j++){
currentCube.push(grid[i][j]);
}
}
return currentCube;
}
// getSubGrid(puzzle, 0, 0); // -> [ 8,9,5,2,7,1,4,6,3 ]
// getSubGrid(puzzle, 1, 0); // -> [ 7,4,2,9,6,3,5,8,1 ]
function includes1to9(section){
//check if no repeats
for(var i=0;i<section.length;i++){
var valueToCheck=section[i];
if(section.indexOf(valueToCheck,i+1)!=-1){
return false;
}
}
//check if contains all numbers in range
var startRange=Math.min(section.join());
for(var i=1;i<=section.length;i++){//range 1-9 is hardcoded
if(section.indexOf(i)<0){
return false;
}
}
return true;
}
//includes1to9([1,2,3,4,5,6,7,8]);
function isSame(puzzleA, puzzleB){
//return true if:
//puzzleA is identical to puzzleB
for(var i=0;i<puzzleA.length;i++){
if(puzzleA[i].join() != puzzleB[i].join()){
return false;
}
}
return true;
}
//isSame(puzzle,puzzle);
//create a sudoku grid
function createAGrid(numCubes,cubeDimension,minIndex,maxIndex){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment