commit 3444cae83879882017dc6b0b72a6fc70cc430357 Author: Dusty Pianalto Date: Tue Jun 9 23:11:53 2020 -0800 Initial commit diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a39f72a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module "github.com/djpianalto/sudokuSolver" + +go 1.14 \ No newline at end of file diff --git a/solver.go b/solver.go new file mode 100644 index 0000000..cf540c6 --- /dev/null +++ b/solver.go @@ -0,0 +1,62 @@ +package sudokuSolver + +import ( + "context" + "math" +) + +func isValid(grid [][]int, x, y, i int) bool { + for n := 0; n < 9; n++ { + if grid[y][n] == i || grid[n][x] == i{ + return false + } + } + topX, topY := int(3 * (math.Floor(float64(x/3)))), int(3 * (math.Floor(float64(y/3)))) + for x := topX; x < topX + 3; x++ { + for y := topY; y < topY + 3; y++ { + if grid[y][x] == i { + return false + } + } + } + return true +} + +func findNextEmptyCell(grid [][]int) (int,int) { + for y := 0; y < 9; y++ { + for x := 0; x < 9; x++ { + if grid[y][x] == 0 { + return x, y + } + } + } + return -1, -1 +} + +func SolveSudoku(grid [][]int, x, y int, ctx context.Context) bool { + select { + case <-ctx.Done(): + return false + default: + } + x, y = findNextEmptyCell(grid) + if x == -1 && y == -1 { + return true + } + for i := 1; i <= 9; i++ { + if isValid(grid, x, y, i) { + grid[y][x] = i + if SolveSudoku(grid, x, y, ctx) { + return true + } + select { + case <-ctx.Done(): + return false + default: + } + grid[y][x] = 0 + } + //time.Sleep(100000000) + } + return false +} \ No newline at end of file