Skip to content

Instantly share code, notes, and snippets.

@eriktrom
Last active August 29, 2015 14:08
Show Gist options
  • Save eriktrom/a019aeebcf5b937bb4de to your computer and use it in GitHub Desktop.
Save eriktrom/a019aeebcf5b937bb4de to your computer and use it in GitHub Desktop.
Validate Sudoku Board Algorithm
define([], function() {
module("Sudoku");
/*
Write a function that takes a 9x9 array of integers and returns true if the following constraints are met:
1. Each row must contain the numbers 1 to 9.
2. Each column must contain the numbers 1 to 9.
3. Each 3 x 3 local square must contain the numbers 1 to 9
http://upload.wikimedia.org/wikipedia/commons/3/31/Sudoku-by-L2G-20050714_solution.svg
*/
var good = [
[5,3,4,6,7,8,9,1,2], //1
[6,7,2,1,9,5,3,4,8], //2
[1,9,8,3,4,2,5,6,7], //3
[8,5,9,7,6,1,4,2,3], //4
[4,2,6,8,5,3,7,9,1], //5
[7,1,3,9,2,4,8,5,6], //6
[9,6,1,5,3,7,2,8,4], //7
[2,8,7,4,1,9,6,3,5], //8
[3,4,5,2,8,6,1,7,9], //9
];
var badRows = [
[5,3,4,6,7,8,9,1,2], //1
[6,7,2,1,9,5,3,4,8], //2
[1,9,8,3,4,2,5,6,7], //3
[8,5,9,7,6,1,4,2,3], //4
[4,2,6,8,5,3,7,9,1], //5
[7,1,3,9,2,4,8,5,6], //6
[9,6,1,5,3,7,2,8,4], //7
[2,8,7,4,1,9,6,3,5], //8
[3,4,5,2,8,6,1,8,9], //9 variant last row repeats 8 twice
];
var badColumns = [
[5,3,4,6,7,8,9,1,2], //1
[6,7,2,1,9,5,3,4,8], //2
[1,9,8,3,4,2,5,6,7], //3
[8,5,9,7,6,1,4,2,3], //4
[4,2,6,8,5,3,7,9,1], //5
[7,1,3,9,2,4,8,5,6], //6
[9,6,1,5,3,7,2,8,4], //7 variant
[2,8,7,4,1,9,6,3,4], //8 variant - last column repeats 4 twice
[3,4,5,2,8,6,1,7,9], //9
];
function validateGroup(group) {
var validNumbers = [1,2,3,4,5,6,7,8,9], currentNum, idx;
for (var i = 0; i < group.length; i++) {
currentNum = group[i];
idx = validNumbers.indexOf(currentNum);
validNumbers[idx] = -1;
if (idx == -1) return false
}
return true;
}
function validateRows(board) {
for (var i = 0; i < board.length; i++) {
if (!validateGroup(board[i])) return false;
}
return true;
}
function validateColumns(board) {
var colNums = [];
var idx = 0;
var currentRow;
while (idx < board.length) {
for (var i = 0; i < board.length; i++) {
currentRow = board[i];
colNums.push(currentRow[idx]);
}
if (!validateGroup(colNums)) return false;
colNums = [];
idx++;
}
return true;
}
function validateBoard(board) {
return validateRows(board) && validateColumns(board);
}
test("validate group of numbers (e.,g, a row, column or 3x3 square)", function() {
var goodGroup = [1,2,3,4,5,6,7,8,9];
var badGroup = [2,2,3,4,5,6,7,8,9];
equal(validateGroup(goodGroup), true);
equal(validateGroup(badGroup), false);
});
test("validate many rows", function() {
equal(validateRows(good), true);
equal(validateRows(badRows), false);
});
test("validate columns", function() {
equal(validateColumns(good), true);
equal(validateColumns(badColumns), false);
});
test("validate board", function() {
equal(validateBoard(good), true);
equal(validateBoard(badRows), false);
equal(validateBoard(badColumns), false);
});
// Note: I'm still looking into how to make a bad 3x3 square AND have
// valid rows and columns at the same time. If it turns out to be possible
// I will update the solution.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment