Skip to content

Instantly share code, notes, and snippets.

@pauloportella
Created August 22, 2018 15:37
Show Gist options
  • Save pauloportella/74adfe35ca2713088ab8b528a46d6383 to your computer and use it in GitHub Desktop.
Save pauloportella/74adfe35ca2713088ab8b528a46d6383 to your computer and use it in GitHub Desktop.
//Turn columns into rows
var transpose = grid =>
grid[0].map(
(_,c) => grid.map(
row => row[c]
)
)
//Turn sub-grids into rows
var expandSubGrids = grid => {
var three = Math.sqrt(grid.length);
return grid[0].map(
(_,i) => grid[0].map(
(_,j) => grid[(i/three|0)*three+j/three|0][i%three*three+j%three]
)
)
}
//Check a given row for duplicates
var checkRow = row => {
var count = [];
for(var cell of row) {
if(cell != '.') {
if(count[cell])
return false;
count[cell] = 1;
}
}
return true;
}
var sudoku2 = grid =>
grid.every(checkRow) &&
transpose(grid).every(checkRow) &&
expandSubGrids(grid).every(checkRow)
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.
Input/Output
[execution time limit] 4 seconds (js)
[input] array.array.char grid
A 9 × 9 array of characters, in which each character is either a digit from '1' to '9' or a period '.'.
[output] boolean
Return true if grid represents a valid Sudoku puzzle, otherwise return false.
[JavaScript (ES6)] Syntax Tips
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment