Last active
September 11, 2019 15:28
-
-
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
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
| 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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to fix the boxCheck index calculation
console.log(Math.floor(i/3)*3 + Math.floor(j/3));