Created
May 7, 2015 21:27
-
-
Save willbuck/052d44b3c35b950ede19 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/niyilumocu/2
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var sudokuValid1 = '4;1,2,3,4,3,4,1,2,2,3,4,1,4,1,2,3'; | |
var sudokuValid2 = '4;2,3,4,1,4,1,2,3,1,2,3,4,3,4,1,2'; | |
var sudokuInvalid1 = '4;1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4'; // invalid rows | |
var sudokuInvalid2 = '4;1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4'; // invalid cols | |
var sudokuInvalid3 = '4;1,2,3,4,2,3,4,1,3,4,1,2,4,1,2,3'; // invalid squares | |
// looked up a valid solution and entered in the format the challenge specified | |
var hardModeValid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,4,1'; | |
// below is same as above, just flipped the last two numbers. it's easy to write an invalid sudoku ;) | |
var hardModeInvalid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,1,4'; | |
// Although in my interview I tried to parse the line into a 2d array, I realized later that wasn't necessary | |
// The trick is really just to find the right offsets to each row, column, and square | |
function validateSudoku(sudokuLine) { | |
// Since we are and-ing all the way down, start with true, any false along the way constitutes a false return | |
var valid = true; | |
var parsedSudokuLine = sudokuLine.split(';'); | |
var n = +parsedSudokuLine[0]; // coerce to int | |
var sudokuArray = parsedSudokuLine[1].split(','); | |
for(var i=0; i < n; i++) { | |
valid = valid && validateIteration(sudokuArray, i, n); | |
// more performant to break if valid ever becomes false | |
if(!valid) { | |
break; | |
} | |
} | |
return valid; | |
} | |
// validates the i-th row, column, and square simultaneously. | |
// Originally I had three validation functions but I noticed they were very similar and | |
// doing them all at once saves time | |
function validateIteration(sudokuArray, itr, n) { | |
var rowInitialOffset = itr*n; | |
var colInitialOffset = itr; | |
// The square offset derivations were much harder than I anticipated | |
var squareInitialOffset = (Math.floor(itr / Math.sqrt(n)) * n * Math.sqrt(n)) + ((itr % Math.sqrt(n)) * Math.sqrt(n)); | |
var expectedArray = []; | |
var actualRowArray = []; | |
var actualColArray = []; | |
var actualSquareArray = []; | |
for(var i=0; i < n; i++) { | |
expectedArray[i] = i+1; | |
var rowOffset = i; | |
var colOffset = i*n; | |
var squareOffset = (Math.floor(i / Math.sqrt(n)) * n) + (i % Math.sqrt(n)); | |
//console.log('itr ' + itr + ' i ' + i + ' init ' + squareInitialOffset + ' offs ' + squareOffset + ' idx ' + (squareInitialOffset + squareOffset)); | |
actualRowArray[i] = +sudokuArray[rowInitialOffset + rowOffset]; | |
actualColArray[i] = +sudokuArray[colInitialOffset + colOffset]; | |
actualSquareArray[i] = +sudokuArray[squareInitialOffset + squareOffset]; | |
} | |
var rowValid = arraysIdentical(expectedArray, actualRowArray.sort()); | |
var colValid = arraysIdentical(expectedArray, actualColArray.sort()); | |
var squareValid = arraysIdentical(expectedArray, actualSquareArray.sort()); | |
return rowValid && colValid && squareValid; | |
} | |
// Borrowed from Stack Overflow, http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript | |
function arraysIdentical(a, b) { | |
var i = a.length; | |
if (i != b.length) return false; | |
while (i--) { | |
if (a[i] !== b[i]) return false; | |
} | |
return true; | |
} | |
// Found this to make more sense as output, unecessary of course but more fun | |
function passFail(booleanExpression) { | |
return booleanExpression ? 'Pass!' : 'Fail :('; | |
} | |
console.log('Expect sudokuValid1 to be valid', passFail(validateSudoku(sudokuValid1) === true)); | |
console.log('Expect sudokuValid2 to be valid', passFail(validateSudoku(sudokuValid2) === true)); | |
console.log('Expect sudokuInvalid1 to be invalid', passFail(validateSudoku(sudokuInvalid1) === false)); | |
console.log('Expect sudokuInvalid2 to be invalid', passFail(validateSudoku(sudokuInvalid2) === false)); | |
console.log('Expect sudokuInvalid3 to be invalid', passFail(validateSudoku(sudokuInvalid3) === false)); | |
console.log('Expect hardModeValid to be valid', passFail(validateSudoku(hardModeValid) === true)); | |
console.log('Expect hardModeInvalid to be invalid', passFail(validateSudoku(hardModeInvalid) === false)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var sudokuValid1 = '4;1,2,3,4,3,4,1,2,2,3,4,1,4,1,2,3'; | |
var sudokuValid2 = '4;2,3,4,1,4,1,2,3,1,2,3,4,3,4,1,2'; | |
var sudokuInvalid1 = '4;1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4'; // invalid rows | |
var sudokuInvalid2 = '4;1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4'; // invalid cols | |
var sudokuInvalid3 = '4;1,2,3,4,2,3,4,1,3,4,1,2,4,1,2,3'; // invalid squares | |
// looked up a valid solution and entered in the format the challenge specified | |
var hardModeValid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,4,1'; | |
// below is same as above, just flipped the last two numbers. it's easy to write an invalid sudoku ;) | |
var hardModeInvalid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,1,4'; | |
// Although in my interview I tried to parse the line into a 2d array, I realized later that wasn't necessary | |
// The trick is really just to find the right offsets to each row, column, and square | |
function validateSudoku(sudokuLine) { | |
// Since we are and-ing all the way down, start with true, any false along the way constitutes a false return | |
var valid = true; | |
var parsedSudokuLine = sudokuLine.split(';'); | |
var n = +parsedSudokuLine[0]; // coerce to int | |
var sudokuArray = parsedSudokuLine[1].split(','); | |
for(var i=0; i < n; i++) { | |
valid = valid && validateIteration(sudokuArray, i, n); | |
// more performant to break if valid ever becomes false | |
if(!valid) { | |
break; | |
} | |
} | |
return valid; | |
} | |
// validates the i-th row, column, and square simultaneously. | |
// Originally I had three validation functions but I noticed they were very similar and | |
// doing them all at once saves time | |
function validateIteration(sudokuArray, itr, n) { | |
var rowInitialOffset = itr*n; | |
var colInitialOffset = itr; | |
// The square offset derivations were much harder than I anticipated | |
var squareInitialOffset = (Math.floor(itr / Math.sqrt(n)) * n * Math.sqrt(n)) + ((itr % Math.sqrt(n)) * Math.sqrt(n)); | |
var expectedArray = []; | |
var actualRowArray = []; | |
var actualColArray = []; | |
var actualSquareArray = []; | |
for(var i=0; i < n; i++) { | |
expectedArray[i] = i+1; | |
var rowOffset = i; | |
var colOffset = i*n; | |
var squareOffset = (Math.floor(i / Math.sqrt(n)) * n) + (i % Math.sqrt(n)); | |
//console.log('itr ' + itr + ' i ' + i + ' init ' + squareInitialOffset + ' offs ' + squareOffset + ' idx ' + (squareInitialOffset + squareOffset)); | |
actualRowArray[i] = +sudokuArray[rowInitialOffset + rowOffset]; | |
actualColArray[i] = +sudokuArray[colInitialOffset + colOffset]; | |
actualSquareArray[i] = +sudokuArray[squareInitialOffset + squareOffset]; | |
} | |
var rowValid = arraysIdentical(expectedArray, actualRowArray.sort()); | |
var colValid = arraysIdentical(expectedArray, actualColArray.sort()); | |
var squareValid = arraysIdentical(expectedArray, actualSquareArray.sort()); | |
return rowValid && colValid && squareValid; | |
} | |
// Borrowed from Stack Overflow, http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript | |
function arraysIdentical(a, b) { | |
var i = a.length; | |
if (i != b.length) return false; | |
while (i--) { | |
if (a[i] !== b[i]) return false; | |
} | |
return true; | |
} | |
// Found this to make more sense as output, unecessary of course but more fun | |
function passFail(booleanExpression) { | |
return booleanExpression ? 'Pass!' : 'Fail :('; | |
} | |
console.log('Expect sudokuValid1 to be valid', passFail(validateSudoku(sudokuValid1) === true)); | |
console.log('Expect sudokuValid2 to be valid', passFail(validateSudoku(sudokuValid2) === true)); | |
console.log('Expect sudokuInvalid1 to be invalid', passFail(validateSudoku(sudokuInvalid1) === false)); | |
console.log('Expect sudokuInvalid2 to be invalid', passFail(validateSudoku(sudokuInvalid2) === false)); | |
console.log('Expect sudokuInvalid3 to be invalid', passFail(validateSudoku(sudokuInvalid3) === false)); | |
console.log('Expect hardModeValid to be valid', passFail(validateSudoku(hardModeValid) === true)); | |
console.log('Expect hardModeInvalid to be invalid', passFail(validateSudoku(hardModeInvalid) === false));</script></body> | |
</html> |
This file contains hidden or 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 sudokuValid1 = '4;1,2,3,4,3,4,1,2,2,3,4,1,4,1,2,3'; | |
var sudokuValid2 = '4;2,3,4,1,4,1,2,3,1,2,3,4,3,4,1,2'; | |
var sudokuInvalid1 = '4;1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4'; // invalid rows | |
var sudokuInvalid2 = '4;1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4'; // invalid cols | |
var sudokuInvalid3 = '4;1,2,3,4,2,3,4,1,3,4,1,2,4,1,2,3'; // invalid squares | |
// looked up a valid solution and entered in the format the challenge specified | |
var hardModeValid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,4,1'; | |
// below is same as above, just flipped the last two numbers. it's easy to write an invalid sudoku ;) | |
var hardModeInvalid = '9;3,9,1,2,8,6,5,7,4,4,8,7,3,5,9,1,2,6,6,5,2,7,1,4,8,3,9,8,7,5,4,3,1,6,9,2,2,1,3,9,6,7,4,8,5,9,6,4,5,2,8,7,1,3,1,4,9,6,7,3,2,5,8,5,3,8,1,4,2,9,6,7,7,2,6,8,9,5,3,1,4'; | |
// Although in my interview I tried to parse the line into a 2d array, I realized later that wasn't necessary | |
// The trick is really just to find the right offsets to each row, column, and square | |
function validateSudoku(sudokuLine) { | |
// Since we are and-ing all the way down, start with true, any false along the way constitutes a false return | |
var valid = true; | |
var parsedSudokuLine = sudokuLine.split(';'); | |
var n = +parsedSudokuLine[0]; // coerce to int | |
var sudokuArray = parsedSudokuLine[1].split(','); | |
for(var i=0; i < n; i++) { | |
valid = valid && validateIteration(sudokuArray, i, n); | |
// more performant to break if valid ever becomes false | |
if(!valid) { | |
break; | |
} | |
} | |
return valid; | |
} | |
// validates the i-th row, column, and square simultaneously. | |
// Originally I had three validation functions but I noticed they were very similar and | |
// doing them all at once saves time | |
function validateIteration(sudokuArray, itr, n) { | |
var rowInitialOffset = itr*n; | |
var colInitialOffset = itr; | |
// The square offset derivations were much harder than I anticipated | |
var squareInitialOffset = (Math.floor(itr / Math.sqrt(n)) * n * Math.sqrt(n)) + ((itr % Math.sqrt(n)) * Math.sqrt(n)); | |
var expectedArray = []; | |
var actualRowArray = []; | |
var actualColArray = []; | |
var actualSquareArray = []; | |
for(var i=0; i < n; i++) { | |
expectedArray[i] = i+1; | |
var rowOffset = i; | |
var colOffset = i*n; | |
var squareOffset = (Math.floor(i / Math.sqrt(n)) * n) + (i % Math.sqrt(n)); | |
//console.log('itr ' + itr + ' i ' + i + ' init ' + squareInitialOffset + ' offs ' + squareOffset + ' idx ' + (squareInitialOffset + squareOffset)); | |
actualRowArray[i] = +sudokuArray[rowInitialOffset + rowOffset]; | |
actualColArray[i] = +sudokuArray[colInitialOffset + colOffset]; | |
actualSquareArray[i] = +sudokuArray[squareInitialOffset + squareOffset]; | |
} | |
var rowValid = arraysIdentical(expectedArray, actualRowArray.sort()); | |
var colValid = arraysIdentical(expectedArray, actualColArray.sort()); | |
var squareValid = arraysIdentical(expectedArray, actualSquareArray.sort()); | |
return rowValid && colValid && squareValid; | |
} | |
// Borrowed from Stack Overflow, http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript | |
function arraysIdentical(a, b) { | |
var i = a.length; | |
if (i != b.length) return false; | |
while (i--) { | |
if (a[i] !== b[i]) return false; | |
} | |
return true; | |
} | |
// Found this to make more sense as output, unecessary of course but more fun | |
function passFail(booleanExpression) { | |
return booleanExpression ? 'Pass!' : 'Fail :('; | |
} | |
console.log('Expect sudokuValid1 to be valid', passFail(validateSudoku(sudokuValid1) === true)); | |
console.log('Expect sudokuValid2 to be valid', passFail(validateSudoku(sudokuValid2) === true)); | |
console.log('Expect sudokuInvalid1 to be invalid', passFail(validateSudoku(sudokuInvalid1) === false)); | |
console.log('Expect sudokuInvalid2 to be invalid', passFail(validateSudoku(sudokuInvalid2) === false)); | |
console.log('Expect sudokuInvalid3 to be invalid', passFail(validateSudoku(sudokuInvalid3) === false)); | |
console.log('Expect hardModeValid to be valid', passFail(validateSudoku(hardModeValid) === true)); | |
console.log('Expect hardModeInvalid to be invalid', passFail(validateSudoku(hardModeInvalid) === false)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment