Skip to content

Instantly share code, notes, and snippets.

@demoive
Last active February 9, 2025 17:16
Show Gist options
  • Save demoive/5177274 to your computer and use it in GitHub Desktop.
Save demoive/5177274 to your computer and use it in GitHub Desktop.
Validates a Sudoku game. The input (vals) is assumed to be an array of arrays with numerical values from 1 through 9.
/**
* Validates a Sudoku game.
* The input <vals> is assumed to be an array of arrays with numerical values from 1 through 9.
*/
function validoku(vals) {
var val,
x, y, blockX,
uniqRow = {},
uniqCols = [{}, {}, {}, {}, {}, {}, {}, {}, {}], // this construct could be "built up" on each loop iteration, but it's done this way for clarity of example
uniqBlocks = [{}, {}, {}];
// iterate through the rows
for (y = 0; y < vals.length; ++y) {
// reset
if (y % 3 === 0) {
uniqBlocks = [{}, {}, {}];
}
// iterate through the columns
for (x = 0; x < vals[y].length; ++x) {
val = vals[y][x];
blockX = (x / 3) | 0; // bitwise operation to ensure integer - equivalent to Math.floor()
// check the validity of the row so far
if (uniqRow[val] === true) {
console.log(val + " is invalid for the row at (" + x + "," + y + ")");
return false;
}
uniqRow[val] = true;
// check the validity of the column so far
if (uniqCols[x][val] === true) {
console.log(val + " is invalid for the column at (" + x + "," + y + ")");
return false;
}
uniqCols[x][val] = true;
// check the validity of the current square
if (uniqBlocks[blockX][val] === true) {
console.log(val + " is invalid for the block at (" + x + "," + y + ")");
return false;
}
uniqBlocks[blockX][val] = true;
}
// reset
uniqRow = {};
}
return true;
}
@demoive
Copy link
Author

demoive commented Mar 16, 2013

Here are some sample inputs for the function

var valid = [
    [8,3,5,4,1,6,9,2,7],
    [2,9,6,8,5,7,4,3,1],
    [4,1,7,2,9,3,6,5,8],
    [5,6,9,1,3,4,7,8,2],
    [1,2,3,6,7,8,5,4,9],
    [7,4,8,5,2,9,1,6,3],
    [6,5,2,7,8,1,3,9,4],
    [9,8,1,3,4,5,2,7,6],
    [3,7,4,9,6,2,8,1,5]
];

var invalidRow = [
    [8,3,5,4,1,6,9,2,7],
    [2,9,6,8,5,7,4,8,1], // (7,1)
    [4,1,7,2,9,3,6,5,8],
    [5,6,9,1,3,4,7,8,2],
    [1,2,3,6,7,8,5,4,9],
    [7,4,8,5,2,9,1,6,3],
    [6,5,2,7,8,1,3,9,4],
    [9,8,1,3,4,5,2,7,6],
    [3,7,4,9,6,2,8,1,5]
];

var invalidCol = [
    [8,3,5,4,1,6,9,2,7],
    [2,9,6,8,5,7,4,3,1],
    [4,1,7,2,9,3,6,5,8],
    [5,1,9,1,3,4,7,8,2], // (1,3)
    [1,2,3,6,7,8,5,4,9],
    [7,4,8,5,2,9,1,6,3],
    [6,5,2,7,8,1,3,9,4],
    [9,8,1,3,4,5,2,7,6],
    [3,7,4,9,6,2,8,1,5]
];

var invalidBlock = [
    [8,3,5,4,1,6,9,2,7],
    [2,9,6,8,5,7,4,3,1],
    [4,1,7,2,9,3,6,5,8],
    [5,6,9,1,3,4,7,8,2],
    [9,2,3,6,7,8,5,4,1], // (0,4)
    [7,4,8,5,2,9,1,6,3],
    [6,5,2,7,8,1,3,9,4],
    [9,8,1,3,4,5,2,7,6],
    [3,7,4,9,6,2,8,1,5]
];
> validoku(valid)
  true
> validoku(invalidRow)
  8 is invalid for the row at (7,1)
  false
> validoku(invalidCol)
  1 is invalid for the column at (1,3)
  false
> validoku(invalidBlock);
  9 is invalid for the block at (0,4)
  false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment