Skip to content

Instantly share code, notes, and snippets.

@holsee
Last active August 29, 2015 14:05
Show Gist options
  • Save holsee/a3fe468c47b2a4764975 to your computer and use it in GitHub Desktop.
Save holsee/a3fe468c47b2a4764975 to your computer and use it in GitHub Desktop.
valid Soduku
function getBox(board, x, y) {
var from = x * 3;
var to = from + 3;
var target = y * 3;
return board[0+target].slice(from, to)
.concat(board[1+target].slice(from, to))
.concat(board[2+target].slice(from, to))
}
function getCol (board, x) {
var tmp = []
for (var i = 0; i < 9; i++) {
tmp.push(board[i][x])
}
return tmp
}
function getRow (board, y) {
return board[y].slice(0)
}
function isValid (subset) {
return subset.sort().join('') == '123456789'
}
function validSolution(board) {
for (var i = 0; i < 9; i++)
if(!isValid(getRow(board, i) || !isValid(getCol(board, i))))
return false;
for (var x = 0; x < 3; x++)
for (var y = 0; y < 3; y++)
if(!isValid(getBox(board, x, y)))
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment