Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sudoku Board Checker
// sudoku board checker
function validateSet(row) {
var hash = {};
if(row.length === 9) {
for(var idx = 0; idx < row.length; idx++) {
hash[row[idx]] = row[idx];
}
for(var i in hash) {
if(hash[i] <= 0 || hash[i] > 9) {
return false;
}
}
} else {
return false;
}
return Object.keys(hash).length === 9 ? true : false;
}
function sudokuIsValid(board) {
console.log('new board-----');
var rows = {};
var cols = {};
var quads = {};
// validate rows and columns
for(var i = 0; i < board.length; i++) {
rows[validateSet(board[i])] = validateSet(board[i]);
var col = [];
for(var j = 0; j < board[i].length; j++) {
col.push(board[j][i]);
}
cols[validateSet(col)] = validateSet(col);
}
console.log('rows', rows);
console.log('cols', cols);
// validate quads
var size = Math.sqrt(board.length),
boxes = [];
for(var r = 0, rl = board.length ; r < rl ; r++) {
var row = board[r];
for(var c = 0, cl = row.length ; c < cl ; c++) {
var box = (r / size | 0) * size + (c / size | 0);
boxes[box] || (boxes[box] = []);
boxes[box].push(row[c]);
}
}
for(var ii in boxes) {
quads[validateSet(boxes[ii])] = validateSet(boxes[ii]);
}
console.log('quads', quads);
if (rows.false || cols.false || quads.false) {
return false;
} else {
return true;
}
}
function test() {
var VALID_SOLUTIONS = [
[
[9, 1, 6, 8, 4, 3, 5, 2, 7],
[8, 4, 2, 7, 5, 6, 9, 3, 1],
[7, 5, 3, 2, 9, 1, 8, 6, 4],
[3, 6, 4, 9, 2, 7, 1, 8, 5],
[2, 8, 1, 5, 6, 4, 7, 9, 3],
[5, 9, 7, 1, 3, 8, 2, 4, 6],
[6, 7, 8, 4, 1, 9, 3, 5, 2],
[4, 2, 9, 3, 7, 5, 6, 1, 8],
[1, 3, 5, 6, 8, 2, 4, 7, 9],
]
]
var INVALID_SOLUTIONS = [
[
[9, 9, 6, 8, 4, 3, 5, 2, 7],
[8, 4, 2, 7, 5, 6, 9, 3, 1],
[7, 5, 3, 2, 9, 1, 8, 6, 4],
[3, 6, 4, 9, 2, 7, 1, 8, 5],
[2, 8, 1, 5, 6, 4, 7, 9, 3],
[5, 9, 7, 1, 3, 8, 2, 4, 6],
[6, 7, 8, 4, 1, 9, 3, 5, 2],
[4, 2, 9, 3, 7, 5, 6, 1, 8],
[1, 3, 5, 6, 8, 2, 4, 7, 9],
],
[
[9, 0, 6, 8, 4, 3, 5, 2, 7],
[8, 4, 2, 7, 5, 6, 9, 3, 1],
[7, 5, 3, 2, 9, 1, 8, 6, 4],
[3, 6, 4, 9, 2, 7, 1, 8, 5],
[2, 8, 1, 5, 6, 4, 7, 9, 3],
[5, 9, 7, 1, 3, 8, 2, 4, 6],
[6, 7, 8, 4, 1, 9, 3, 5, 2],
[4, 2, 9, 3, 7, 5, 6, 1, 8],
[1, 3, 5, 6, 8, 2, 4, 7, 9],
],
[
[9, 1, 6, 8, 4, 3, 5, 2, 7],
[8, 4, 2, 7, 5, 6, 9, 3, 1],
[7, 5, 9, 2, 9, 1, 8, 6, 4],
[3, 6, 4, 9, 2, 7, 1, 8, 5],
[2, 8, 1, 5, 6, 4, 7, 9, 3],
[5, 9, 7, 1, 3, 8, 2, 4, 6],
[6, 7, 8, 4, 1, 9, 3, 5, 2],
[4, 2, 9, 3, 7, 5, 6, 1, 8],
[1, 3, 5, 6, 8, 2, 4, 7, 9],
],
];
for (var idx in VALID_SOLUTIONS) {
var validSolution = VALID_SOLUTIONS[idx];
if (!sudokuIsValid(validSolution)) {
throw new Error('Valid solution failed');
}
}
var invalidSolutions = [];
for (var idx in INVALID_SOLUTIONS) {
var invalidSolution = INVALID_SOLUTIONS[idx];
if (sudokuIsValid(invalidSolution)) {
invalidSolutions.push(new Error('Invalid solution failed'));
}
}
console.log('invalidSolutions', invalidSolutions);
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.