Generate all possible sudoku solutions. (May take a few years to finish).
This commit is contained in:
parent
89131899d1
commit
35ec91a487
10
.pre-commit-config.yaml
Normal file
10
.pre-commit-config.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
# See https://pre-commit.com for more information
|
||||
# See https://pre-commit.com/hooks.html for more hooks
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v3.2.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
- id: check-yaml
|
||||
- id: check-added-large-files
|
17
Taskfile.yml
Normal file
17
Taskfile.yml
Normal file
@ -0,0 +1,17 @@
|
||||
# https://taskfile.dev
|
||||
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
default:
|
||||
cmds:
|
||||
- go run .
|
||||
silent: true
|
||||
precommit:
|
||||
cmds:
|
||||
- pre-commit run --all
|
||||
silent: true
|
||||
build:
|
||||
cmds:
|
||||
- go build .
|
||||
silent: true
|
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module gitea.ligthert.net/golang/sudoku-solver
|
||||
|
||||
go 1.23.4
|
7
main.go
Normal file
7
main.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "gitea.ligthert.net/golang/sudoku-solver/solver"
|
||||
|
||||
func main() {
|
||||
solver.Run()
|
||||
}
|
362880
solver/blocks.csv
Normal file
362880
solver/blocks.csv
Normal file
File diff suppressed because it is too large
Load Diff
17
solver/blocks.go
Normal file
17
solver/blocks.go
Normal file
@ -0,0 +1,17 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed blocks.csv
|
||||
var fileString string
|
||||
|
||||
func (solver *Solver) load_blocks() {
|
||||
for _, line := range strings.Split(strings.TrimSuffix(fileString, "\n"), "\n") {
|
||||
block, _ := strconv.Atoi(line)
|
||||
solver.blocks = append(solver.blocks, block)
|
||||
}
|
||||
}
|
295
solver/solver.go
Normal file
295
solver/solver.go
Normal file
@ -0,0 +1,295 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
solver := Solver{}
|
||||
solver.load_blocks()
|
||||
|
||||
for index1 := range solver.blocks {
|
||||
for index2 := range solver.blocks {
|
||||
if solver.compare2(index1, index2) {
|
||||
for index3 := range solver.blocks {
|
||||
if solver.compare3(index1, index2, index3) {
|
||||
for index4 := range solver.blocks {
|
||||
if solver.compare4(index1, index2, index3, index4) {
|
||||
for index5 := range solver.blocks {
|
||||
if solver.compare5(index1, index2, index3, index4, index5) {
|
||||
for index6 := range solver.blocks {
|
||||
if solver.compare6(index1, index2, index3, index4, index5, index6) {
|
||||
for index7 := range solver.blocks {
|
||||
if solver.compare7(index1, index2, index3, index4, index5, index6, index7) {
|
||||
for index8 := range solver.blocks {
|
||||
if solver.compare8(index1, index2, index3, index4, index5, index6, index7, index8) {
|
||||
for index9 := range solver.blocks {
|
||||
go solver.routine_compare9(index1, index2, index3, index4, index5, index6, index7, index8, index9)
|
||||
// if solver.compare9(index1, index2, index3, index4, index5, index6, index7, index8, index9) {
|
||||
// fmt.Printf("%d:%d:%d:%d:%d:%d:%d:%d:%d\n", solver.blocks[index1], solver.blocks[index2], solver.blocks[index3], solver.blocks[index4], solver.blocks[index5], solver.blocks[index6], solver.blocks[index7], solver.blocks[index8], solver.blocks[index9])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (solver *Solver) compare2(index1 int, index2 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare3(index1 int, index2 int, index3 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] {
|
||||
retval = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare4(index1 int, index2 int, index3 int, index4 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare5(index1 int, index2 int, index3 int, index4 int, index5 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
row5 := strconv.Itoa(solver.blocks[index5])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] || row1[index] == row5[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] || row2[index] == row5[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] || row3[index] == row5[index] {
|
||||
retval = false
|
||||
}
|
||||
if row4[index] == row5[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare6(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
row5 := strconv.Itoa(solver.blocks[index5])
|
||||
row6 := strconv.Itoa(solver.blocks[index6])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] || row1[index] == row5[index] || row1[index] == row6[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] || row2[index] == row5[index] || row2[index] == row6[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] || row3[index] == row5[index] || row3[index] == row6[index] {
|
||||
retval = false
|
||||
}
|
||||
if row4[index] == row5[index] || row4[index] == row6[index] {
|
||||
retval = false
|
||||
}
|
||||
if row5[index] == row6[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare7(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
row5 := strconv.Itoa(solver.blocks[index5])
|
||||
row6 := strconv.Itoa(solver.blocks[index6])
|
||||
row7 := strconv.Itoa(solver.blocks[index7])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] || row1[index] == row5[index] || row1[index] == row6[index] || row1[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] || row2[index] == row5[index] || row2[index] == row6[index] || row2[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] || row3[index] == row5[index] || row3[index] == row6[index] || row3[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
if row4[index] == row5[index] || row4[index] == row6[index] || row4[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
if row5[index] == row6[index] || row5[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
if row6[index] == row7[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) compare8(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int, index8 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
row5 := strconv.Itoa(solver.blocks[index5])
|
||||
row6 := strconv.Itoa(solver.blocks[index6])
|
||||
row7 := strconv.Itoa(solver.blocks[index7])
|
||||
row8 := strconv.Itoa(solver.blocks[index8])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] || row1[index] == row5[index] || row1[index] == row6[index] || row1[index] == row7[index] || row1[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] || row2[index] == row5[index] || row2[index] == row6[index] || row2[index] == row7[index] || row2[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] || row3[index] == row5[index] || row3[index] == row6[index] || row3[index] == row7[index] || row3[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row4[index] == row5[index] || row4[index] == row6[index] || row4[index] == row7[index] || row4[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row5[index] == row6[index] || row5[index] == row7[index] || row5[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row6[index] == row7[index] || row6[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
if row7[index] == row8[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) routine_compare9(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int, index8 int, index9 int) {
|
||||
|
||||
if solver.compare9(index1, index2, index3, index4, index5, index6, index7, index8, index9) {
|
||||
fmt.Printf("%d:%d:%d:%d:%d:%d:%d:%d:%d\n", solver.blocks[index1], solver.blocks[index2], solver.blocks[index3], solver.blocks[index4], solver.blocks[index5], solver.blocks[index6], solver.blocks[index7], solver.blocks[index8], solver.blocks[index9])
|
||||
}
|
||||
}
|
||||
|
||||
func (solver *Solver) compare9(index1 int, index2 int, index3 int, index4 int, index5 int, index6 int, index7 int, index8 int, index9 int) bool {
|
||||
var retval bool
|
||||
retval = true
|
||||
|
||||
row1 := strconv.Itoa(solver.blocks[index1])
|
||||
row2 := strconv.Itoa(solver.blocks[index2])
|
||||
row3 := strconv.Itoa(solver.blocks[index3])
|
||||
row4 := strconv.Itoa(solver.blocks[index4])
|
||||
row5 := strconv.Itoa(solver.blocks[index5])
|
||||
row6 := strconv.Itoa(solver.blocks[index6])
|
||||
row7 := strconv.Itoa(solver.blocks[index7])
|
||||
row8 := strconv.Itoa(solver.blocks[index8])
|
||||
row9 := strconv.Itoa(solver.blocks[index9])
|
||||
|
||||
for index := range 9 {
|
||||
if row1[index] == row2[index] || row1[index] == row3[index] || row1[index] == row4[index] || row1[index] == row5[index] || row1[index] == row6[index] || row1[index] == row7[index] || row1[index] == row8[index] || row1[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row2[index] == row3[index] || row2[index] == row4[index] || row2[index] == row5[index] || row2[index] == row6[index] || row2[index] == row7[index] || row2[index] == row8[index] || row2[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row3[index] == row4[index] || row3[index] == row5[index] || row3[index] == row6[index] || row3[index] == row7[index] || row3[index] == row8[index] || row3[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row4[index] == row5[index] || row4[index] == row6[index] || row4[index] == row7[index] || row4[index] == row8[index] || row4[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row5[index] == row6[index] || row5[index] == row7[index] || row5[index] == row8[index] || row5[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row6[index] == row7[index] || row6[index] == row8[index] || row7[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row7[index] == row8[index] || row7[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
if row8[index] == row9[index] {
|
||||
retval = false
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
5
solver/types.go
Normal file
5
solver/types.go
Normal file
@ -0,0 +1,5 @@
|
||||
package solver
|
||||
|
||||
type Solver struct {
|
||||
blocks []int
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user