First commit
This commit is contained in:
commit
1bc9ef6af2
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -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.
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# sudoku-solver
|
||||||
|
This tool tries to find a solution for a given sudoku grid.
|
98
main.go
Normal file
98
main.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user