Skip to content

Instantly share code, notes, and snippets.

@andersonleite
Created July 17, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersonleite/ce65bdaa113f1db059941db2bdbba2b4 to your computer and use it in GitHub Desktop.
Save andersonleite/ce65bdaa113f1db059941db2bdbba2b4 to your computer and use it in GitHub Desktop.
Sudoku
// Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with numbers in such a way that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid all contain all of the numbers from 1 to 9 one time.
// Implement an algorithm that will check whether the given grid of numbers represents a valid Sudoku puzzle according to the layout rules described above. Note that the puzzle represented by grid does not have to be solvable.
// Example
// For
// grid = [['.', '.', '.', '1', '4', '.', '.', '2', '.'],
// ['.', '.', '6', '.', '.', '.', '.', '.', '.'],
// ['.', '.', '.', '.', '.', '.', '.', '.', '.'],
// ['.', '.', '1', '.', '.', '.', '.', '.', '.'],
// ['.', '6', '7', '.', '.', '.', '.', '.', '9'],
// ['.', '.', '.', '.', '.', '.', '8', '1', '.'],
// ['.', '3', '.', '.', '.', '.', '.', '.', '6'],
// ['.', '.', '.', '.', '.', '7', '.', '.', '.'],
// ['.', '.', '.', '5', '.', '.', '.', '7', '.']]
// the output should be
// sudoku2(grid) = true;
// For
// grid = [['.', '.', '.', '.', '2', '.', '.', '9', '.'],
// ['.', '.', '.', '.', '6', '.', '.', '.', '.'],
// ['7', '1', '.', '.', '7', '5', '.', '.', '.'],
// ['.', '7', '.', '.', '.', '.', '.', '.', '.'],
// ['.', '.', '.', '.', '8', '3', '.', '.', '.'],
// ['.', '.', '8', '.', '.', '7', '.', '6', '.'],
// ['.', '.', '.', '.', '.', '2', '.', '.', '.'],
// ['.', '1', '.', '2', '.', '.', '.', '.', '.'],
// ['.', '2', '.', '.', '3', '.', '.', '.', '.']]
// the output should be
// sudoku2(grid) = false.
// The given grid is not correct because there are two 1s in the second column. Each column, each row, and each 3 × 3 subgrid can only contain the numbers 1 through 9 one time.
function sudoku2(grid) {
// validate rows
for(let i=0; i<grid.length; i++){
if(validate(grid[i])){
return false
}
}
// validate columns
for(let i=0; i<grid.length; i++){
let col = []
for(let j=0; j<grid.length; j++){
col.push(grid[j][i])
}
if(validate(col)){
return false
}
}
// validate 3x3
for(let i=0; i<grid.length-2; i+=3){
for(let j=0; j<grid.length-2; j+=3){
let miniGrid = getMiniGrid(grid, i, j)
if(validate(miniGrid)){
return false
}
}
}
return true
}
const validate = (data) => {
const numbers = data.filter((r)=>{
if(r!=='.' && data.indexOf(r)!==data.lastIndexOf(r)){
return r
}
})
return numbers.length > 1
}
const getMiniGrid = (grid, row, col) => {
let miniGrid = []
for(let i=row; i<row+3; i++){
for(let j=col; j<col+3; j++){
miniGrid.push(grid[j][i])
}
}
return miniGrid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment