Last active
October 19, 2016 17:32
-
-
Save andrewgremlich/eebded77dc21aa9388105de597f35db7 to your computer and use it in GitHub Desktop.
Code to see if an array of a sudoku number set is valid.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compare3x3(data) { | |
"use strict"; | |
var chunkLength = data.length / 3, | |
iter = 1, | |
objarr = { | |
"array1": [], | |
"array2": [], | |
"array3": [], | |
"array4": [], | |
"array5": [], | |
"array6": [], | |
"array7": [], | |
"array8": [], | |
"array9": [] | |
}; | |
for (var i = 0; i < 3; i++) { | |
var chunk = data.slice(i * chunkLength, chunkLength * (i + 1)); | |
var iter = 1; | |
for (var j = 0; j < chunkLength; j += 3) { | |
var t1 = chunk[j], | |
t2 = chunk[j + 1], | |
t3 = chunk[j + 2]; | |
if (iter === 4) { | |
iter = 1; | |
} | |
objarr["array" + ((i * 3) + iter)].push(t1); | |
objarr["array" + ((i * 3) + iter)].push(t2); | |
objarr["array" + ((i * 3) + iter)].push(t3); | |
iter++; | |
} | |
} | |
for (var k in objarr) { | |
var arrayhold = objarr[k].sort(); | |
for (var l = 0; l < arrayhold.length; l++) { | |
if (arrayhold[l + 1] === arrayhold[l]) { | |
return k; | |
} | |
} | |
return true; | |
} | |
} | |
module.exports = compare3x3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compareColumns(data) { | |
for (var i = 0; i < 9; i++) { | |
var arrayHold = []; | |
for (var j = 0; j < 9; j++) { | |
arrayHold.push(data[i + (9 * j)]); | |
} | |
var temp = arrayHold.sort(); | |
for (var k = 0; k < temp.length; k++) { | |
if (temp[k + 1] === temp[k]) { | |
return i+1; | |
} | |
} | |
return true; | |
} | |
} | |
module.exports = compareColumns; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compareRows(data) { | |
var temp, | |
chunk = 9; | |
for (var i = 0; i < data.length; i+= chunk) { | |
temp = data.slice(i, i + chunk); | |
var tempArray = temp, | |
sortedArr = tempArray.sort(); | |
for (var j = 0; j < sortedArr.length; j++) { | |
if (sortedArr[j + 1] === sortedArr[j]) { | |
return i+1; | |
} | |
} | |
return true; | |
} | |
}; | |
module.exports = compareRows; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var compareRows = require('./comparerows.js'), | |
compareColumns = require('./comparecolumns.js'), | |
compare3x3 = require('./compare3x3.js'), | |
linesTrue = [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]; | |
var verifyRows = compareRows(linesTrue), | |
verifyColumns = compareColumns(linesTrue), | |
verify3x3 = compare3x3(linesTrue); | |
if (verifyColumns === true && verifyRows === true && verify3x3 === true) { | |
process.stdout.write("This sudoku set is true"); | |
} else { | |
console.log("Error in Column " + verifyColumns); | |
console.log("Error in Row " + verifyRows); | |
console.log("Error in 3x3 " + verify3x3); | |
process.stdout.write("This sudoku set is all lies!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment