commit 1bc9ef6af236ed4c4b0ca13abc6d352f48fc2a01 Author: adrien Date: Wed Mar 2 00:05:35 2022 +0100 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf5c428 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Adrien + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2bf7b1e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# sudoku-solver +This tool tries to find a solution for a given sudoku grid. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1028774 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.illuad.fr/adrien/sudoku-solver + +go 1.17 diff --git a/main.go b/main.go new file mode 100644 index 0000000..17b313f --- /dev/null +++ b/main.go @@ -0,0 +1,98 @@ +package main + +import "fmt" + +var field = [9][9]int{ + {5, 3, 0, 0, 7, 0, 0, 0, 0}, + {6, 0, 0, 1, 9, 5, 0, 0, 0}, + {0, 9, 8, 0, 0, 0, 0, 6, 0}, + {8, 0, 0, 0, 6, 0, 0, 0, 3}, + {4, 0, 0, 8, 0, 3, 0, 0, 1}, + {7, 0, 0, 0, 2, 0, 0, 0, 6}, + {0, 6, 0, 0, 0, 0, 2, 8, 0}, + {0, 0, 0, 4, 1, 9, 0, 0, 5}, + {0, 0, 0, 0, 8, 0, 0, 7, 9}, +} + +func draw() { + for _, row := range field { + fmt.Println(row) + } + fmt.Println() +} + +func canPut(x int, y int, value int) bool { + return !alreadyInVertical(x, y, value) && + !alreadyInHorizontal(x, y, value) && + !alreadyInSquare(x, y, value) +} + +func alreadyInVertical(x int, y int, value int) bool { + for i := range [9]int{} { + if field[i][x] == value { + return true + } + } + return false +} + +func alreadyInHorizontal(x int, y int, value int) bool { + for i := range [9]int{} { + if field[y][i] == value { + return true + } + } + return false +} + +func alreadyInSquare(x int, y int, value int) bool { + sx, sy := int(x/3)*3, int(y/3)*3 + for dy := range [3]int{} { + for dx := range [3]int{} { + if field[sy+dy][sx+dx] == value { + return true + } + } + } + return false +} + +func next(x int, y int) (int, int) { + nextX, nextY := (x+1)%9, y + if nextX == 0 { + nextY = y + 1 + } + return nextX, nextY +} + +func solve(x int, y int) bool { + if y == 9 { + return true + } + if field[y][x] != 0 { + return solve(next(x, y)) + } else { + for i := range [9]int{} { + var v = i + 1 + if canPut(x, y, v) { + field[y][x] = v + if solve(next(x, y)) { + return true + } + field[y][x] = 0 + } + } + return false + } +} + +func main() { + fmt.Println("Grid to be completed:") + draw() + if solve(0, 0) { + fmt.Println("I found a solution, see below") + draw() + } else { + fmt.Println("I did not find a solution") + } +}