Skip to content

Instantly share code, notes, and snippets.

@unacorbatanegra
Created July 21, 2021 19:23
Show Gist options
  • Save unacorbatanegra/189ffd834ac09c601e9d1e0e4f22fe7b to your computer and use it in GitHub Desktop.
Save unacorbatanegra/189ffd834ac09c601e9d1e0e4f22fe7b to your computer and use it in GitHub Desktop.
sudoku validator
// 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.
void main(){
final grid = [
['.', '.', '.', '1', '4', '.', '.', '2', '.'],
['.', '.', '6', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '1', '.', '.', '.', '.', '.', '.'],
['.', '6', '7', '.', '.', '.', '.', '.', '9'],
['.', '.', '.', '.', '.', '.', '8', '1', '.'],
['.', '3', '.', '.', '.', '.', '.', '.', '6'],
['.', '.', '.', '.', '.', '7', '.', '.', '.'],
['.', '.', '.', '5', '.', '.', '.', '7', '.']
];
final grid2 = [
['.', '.', '.', '.', '2', '.', '.', '9', '.'],
['.', '.', '.', '.', '6', '.', '.', '.', '.'],
['7', '1', '.', '.', '7', '5', '.', '.', '.'],
['.', '7', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '8', '3', '.', '.', '.'],
['.', '.', '8', '.', '.', '7', '.', '6', '.'],
['.', '.', '.', '.', '.', '2', '.', '.', '.'],
['.', '1', '.', '2', '.', '.', '.', '.', '.'],
['.', '2', '.', '.', '3', '.', '.', '.', '.']
];
final grid3 = [
['.', '4', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '4', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '1', '.', '.', '7', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '3', '.', '.', '.', '6', '.'],
['.', '.', '.', '.', '.', '6', '.', '9', '.'],
['.', '.', '.', '.', '1', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '2', '.', '.'],
['.', '.', '.', '8', '.', '.', '.', '.', '.']
];
print(sudoku2(grid));
print(sudoku2(grid2));
print(sudoku2(grid3));
}
bool sudoku2(List<List<String>> grid) {
final n = grid.length;
var result = true;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
final current = grid[row][col];
final startR = row + 3;
final startC = col + 3;
if ((startC % 3) == 0 && (startR % 3) == 0) {
final map = <String, int>{};
for (var r = row; r < startR; r++) {
for (var c = col; c < startC; c++) {
final cc = grid[r][c];
if (cc == '.') continue;
if (map[cc] == null) {
map[cc] = -1;
} else {
return false;
}
}
}
}
if (current == '.') continue;
for (int c = (col + 1); c < n; c++) {
final cc = grid[row][c];
if (cc == '.') continue;
if( current == cc)return false;
}
for (int r = row + 1; r < n; r++) {
final cc = grid[r][col];
if (cc == '.') continue;
if( current == cc)return false;
if (!result) return result;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment