Last active
January 20, 2024 07:04
-
-
Save maxx1128/c5c62dd09291f10bc7e8c0b77df80dbb to your computer and use it in GitHub Desktop.
Check if you solved a Sudoku
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
const checkSudoku = board => { | |
const isSudokuArrayValid = (array) => { | |
const row = array.slice(0).sort().join(''), | |
passingRow = [1,2,3,4,5,6,7,8,9].join(''); | |
return (row === passingRow); | |
}; | |
const testRows = (board) => board.every(row => isSudokuArrayValid(row)); | |
const testColumns = (board) => { | |
const gatherColumn = (board, columnNum) => board.reduce((total, row) => [...total, row[columnNum]], []); | |
return [0,1,2,3,4,5,6,7,8].every(i => isSudokuArrayValid(gatherColumn(board, i))); | |
} | |
const testSquares = (board) => { | |
const getSquareIndexes = (num) => { | |
if (num === 1) { | |
return [0,1,2]; | |
} else if (num === 2) { | |
return [3,4,5]; | |
} else { | |
return [6,7,8]; | |
} | |
} | |
const getSquareValues = (x, y, board) => { | |
let values = [], | |
rows = getSquareIndexes(x), | |
columns = getSquareIndexes(y); | |
rows.forEach(row => { | |
columns.forEach(column => { | |
values.push(board[row][column]); | |
}); | |
}); | |
return values; | |
}; | |
const squareSections = [1,2,3]; | |
return squareSections.every(squareX => { | |
return squareSections.every(squareY => isSudokuArrayValid(getSquareValues(squareX, squareY, board))); | |
}); | |
}; | |
return (testRows(board) && testColumns(board) && testSquares(board)) ? 'Finished!' : 'Try again!'; | |
} | |
const pass = checkSudoku([ | |
[5, 3, 4, 6, 7, 8, 9, 1, 2], | |
[6, 7, 2, 1, 9, 5, 3, 4, 8], | |
[1, 9, 8, 3, 4, 2, 5, 6, 7], | |
[8, 5, 9, 7, 6, 1, 4, 2, 3], | |
[4, 2, 6, 8, 5, 3, 7, 9, 1], | |
[7, 1, 3, 9, 2, 4, 8, 5, 6], | |
[9, 6, 1, 5, 3, 7, 2, 8, 4], | |
[2, 8, 7, 4, 1, 9, 6, 3, 5], | |
[3, 4, 5, 2, 8, 6, 1, 7, 9] | |
]); | |
const fail = checkSudoku([ | |
[5, 3, 4, 6, 7, 8, 9, 1, 2], | |
[6, 7, 2, 1, 9, 0, 3, 4, 9], | |
[1, 0, 0, 3, 4, 2, 5, 6, 0], | |
[8, 5, 9, 7, 6, 1, 0, 2, 0], | |
[4, 2, 6, 8, 5, 3, 7, 9, 1], | |
[7, 1, 3, 9, 2, 4, 8, 5, 6], | |
[9, 0, 1, 5, 3, 7, 2, 1, 4], | |
[2, 8, 7, 4, 1, 9, 6, 3, 5], | |
[3, 0, 0, 4, 8, 1, 1, 7, 9] | |
]); | |
console.log(pass); | |
// 'Finished!' | |
console.log(fail); | |
// 'Try again!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment