Skip to content

Instantly share code, notes, and snippets.

@WizardOfArc
Last active September 11, 2019 15:28
Show Gist options
  • Select an option

  • Save WizardOfArc/48c5352b1bb09570125128f0ed09e5f1 to your computer and use it in GitHub Desktop.

Select an option

Save WizardOfArc/48c5352b1bb09570125128f0ed09e5f1 to your computer and use it in GitHub Desktop.
function to test a 9 x 9 array of integers to see if it's a winning Sudoku board
const checkSudoku = (board) => {
let rowCheck = new Array(9).fill(1);
let colCheck = new Array(9).fill(1);
let boxCheck = new Array(9).fill(1);
for(let row = 0; row < 9; row++){
for(let col = 0; col < 9; col++){
if( rowCheck[row] &= (1 << board[row][col]) != 0){
return false;
}
rowCheck[row] |= (1 << board[row][col]);
if( colCheck[col] &= (1 << board[row][col]) != 0){
return false;
}
colCheck[col] |= (1 << board[row][col]);
if ( boxCheck[Math.floor(row/3)*3 + Math.floor(col/3)] &= (1 << board[row][col]) != 0){
return false;
}
boxCheck[Math.floor(row/3)*3 + Math.floor(col/3)] |= (1 << board[row][col]);
}
}
return true;
}
@WizardOfArc
Copy link
Copy Markdown
Author

I need to fix the boxCheck index calculation

console.log(Math.floor(i/3)*3 + Math.floor(j/3));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment