Created
November 13, 2023 22:39
-
-
Save danny-andrews/fedcde6da378c9d9151254fe89063440 to your computer and use it in GitHub Desktop.
Sudoku Checker Solution
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
export const checkSudoku = (board) => { | |
for (let i = 0; i < 4; i++) { | |
const row = new Set(); | |
const col = new Set(); | |
const square = new Set(); | |
for (let j = 0; j < 4; j++) { | |
const rowEl = board[i][j]; | |
const colEl = board[j][i]; | |
const squareEl = | |
board[2 * Math.floor(i / 2) + Math.floor(j / 2)][2 * (i % 2) + (j % 2)]; | |
if (row.has(rowEl) || col.has(colEl) || square.has(squareEl)) { | |
return false; | |
} | |
row.add(rowEl); | |
col.add(colEl); | |
square.add(squareEl); | |
} | |
} | |
return true; | |
}; | |
export const checkSudokuFull = (board) => { | |
for (let i = 0; i < 9; i++) { | |
const row = new Set(); | |
const col = new Set(); | |
const square = new Set(); | |
for (let j = 0; j < 9; j++) { | |
const rowEl = board[i][j]; | |
const colEl = board[j][i]; | |
const squareEl = | |
board[3 * Math.floor(i / 3) + Math.floor(j / 3)][3 * (i % 3) + (j % 3)]; | |
if (row.has(rowEl) || col.has(colEl) || square.has(squareEl)) { | |
return false; | |
} | |
row.add(rowEl); | |
col.add(colEl); | |
square.add(squareEl); | |
} | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment