sudoku-funpark/main.go

282 lines
8.6 KiB
Go
Raw Normal View History

2024-12-05 17:46:09 +01:00
package main
import (
2024-12-06 19:28:24 +01:00
"encoding/csv"
2024-12-05 17:46:09 +01:00
"fmt"
2024-12-06 19:28:24 +01:00
"io"
"log"
"os"
2024-12-05 17:46:09 +01:00
"strconv"
)
func main() {
2024-12-06 19:28:24 +01:00
blocks := load_blocks()
2024-12-06 21:37:15 +01:00
// fmt.Println("Total blocks:", len(blocks))
2024-12-06 19:28:24 +01:00
// Parse parameters
// Turn parameters into rows
row1 := "769104803"
2024-12-06 21:37:15 +01:00
row2 := "154800060"
row3 := "002700150"
row4 := "600900308"
row5 := "045328670"
row6 := "328670945"
row7 := "597410280"
row8 := "006284090"
row9 := "200590006"
2024-12-06 19:28:24 +01:00
// Find rows that fit with the entered rows
rows1 := find_blocks(blocks, row1)
2024-12-06 21:37:15 +01:00
rows2 := find_blocks(blocks, row2)
rows3 := find_blocks(blocks, row3)
rows4 := find_blocks(blocks, row4)
rows5 := find_blocks(blocks, row5)
rows6 := find_blocks(blocks, row6)
rows7 := find_blocks(blocks, row7)
rows8 := find_blocks(blocks, row8)
rows9 := find_blocks(blocks, row9)
2024-12-06 19:28:24 +01:00
// fmt.Println(rows1)
2024-12-06 21:37:15 +01:00
// fmt.Println(rows2)
// fmt.Println(rows3)
// fmt.Println(rows4)
// fmt.Println(rows5)
// fmt.Println(rows6)
// fmt.Println(rows7)
// fmt.Println(rows8)
// fmt.Println(rows9)
// fmt.Println("len(rows1):", len(rows1))
// fmt.Println("len(rows2):", len(rows2))
// fmt.Println("len(rows3):", len(rows3))
// fmt.Println("len(rows4):", len(rows4))
// fmt.Println("len(rows5):", len(rows5))
// fmt.Println("len(rows6):", len(rows6))
// fmt.Println("len(rows7):", len(rows7))
// fmt.Println("len(rows8):", len(rows8))
// fmt.Println("len(rows9):", len(rows9))
fmt.Println("Number of iterations:", len(rows1)*len(rows2)*len(rows3)*len(rows4)*len(rows5)*len(rows6)*len(rows7)*len(rows8)*len(rows9))
iter := len(rows1) * len(rows2) * len(rows3) * len(rows4) * len(rows5) * len(rows6) * len(rows7) * len(rows8) * len(rows9)
solutions := check_combinations(rows1, rows2, rows3, rows4, rows5, rows6, rows7, rows8, rows9, iter)
fmt.Println(solutions)
}
func check_combinations(rows1 []int, rows2 []int, rows3 []int, rows4 []int, rows5 []int, rows6 []int, rows7 []int, rows8 []int, rows9 []int, iter int) []string {
var combinations []string
var counter int
var percentage float32
for rows1_index := range rows1 {
for rows2_index := range rows2 {
for rows3_index := range rows3 {
for rows4_index := range rows4 {
for rows5_index := range rows5 {
for rows6_index := range rows6 {
for rows7_index := range rows7 {
for rows8_index := range rows8 {
for rows9_index := range rows9 {
if validate_combination(rows1[rows1_index], rows2[rows2_index], rows3[rows3_index], rows4[rows4_index], rows5[rows5_index], rows6[rows6_index], rows7[rows7_index], rows8[rows8_index], rows9[rows9_index]) {
combinations = append(combinations, render_combination(rows1[rows1_index], rows2[rows2_index], rows3[rows3_index], rows4[rows4_index], rows5[rows5_index], rows6[rows6_index], rows7[rows7_index], rows8[rows8_index], rows9[rows9_index]))
}
counter = counter + 1
if counter%1000000 == 0 {
percentage = (float32(counter) / (float32(iter) / 100))
fmt.Println("Processing:", percentage, "%")
}
}
}
}
}
}
}
}
}
}
return combinations
}
func validate_combination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) bool {
var retval bool
retval = true
row1s := strconv.Itoa(row1)
row2s := strconv.Itoa(row2)
row3s := strconv.Itoa(row3)
row4s := strconv.Itoa(row4)
row5s := strconv.Itoa(row5)
row6s := strconv.Itoa(row6)
row7s := strconv.Itoa(row7)
row8s := strconv.Itoa(row8)
row9s := strconv.Itoa(row9)
for index := range 9 {
if row1s[index] == row2s[index] || row1s[index] == row3s[index] || row1s[index] == row4s[index] || row1s[index] == row5s[index] || row1s[index] == row6s[index] || row1s[index] == row7s[index] || row1s[index] == row8s[index] || row1s[index] == row9s[index] {
retval = false
}
if row2s[index] == row1s[index] || row2s[index] == row3s[index] || row2s[index] == row4s[index] || row2s[index] == row5s[index] || row2s[index] == row6s[index] || row2s[index] == row7s[index] || row2s[index] == row8s[index] || row2s[index] == row9s[index] {
retval = false
}
if row3s[index] == row1s[index] || row3s[index] == row2s[index] || row3s[index] == row4s[index] || row3s[index] == row5s[index] || row3s[index] == row6s[index] || row3s[index] == row7s[index] || row3s[index] == row8s[index] || row3s[index] == row9s[index] {
retval = false
}
if row4s[index] == row1s[index] || row4s[index] == row2s[index] || row4s[index] == row3s[index] || row4s[index] == row5s[index] || row4s[index] == row6s[index] || row4s[index] == row7s[index] || row4s[index] == row8s[index] || row4s[index] == row9s[index] {
retval = false
}
if row5s[index] == row1s[index] || row5s[index] == row2s[index] || row5s[index] == row3s[index] || row5s[index] == row4s[index] || row5s[index] == row6s[index] || row5s[index] == row7s[index] || row5s[index] == row8s[index] || row5s[index] == row9s[index] {
retval = false
}
if row6s[index] == row1s[index] || row6s[index] == row2s[index] || row6s[index] == row3s[index] || row6s[index] == row4s[index] || row6s[index] == row5s[index] || row6s[index] == row7s[index] || row6s[index] == row8s[index] || row6s[index] == row9s[index] {
retval = false
}
if row7s[index] == row1s[index] || row7s[index] == row2s[index] || row7s[index] == row3s[index] || row7s[index] == row4s[index] || row5s[index] == row6s[index] || row7s[index] == row6s[index] || row7s[index] == row8s[index] || row7s[index] == row9s[index] {
retval = false
}
if row8s[index] == row1s[index] || row8s[index] == row2s[index] || row8s[index] == row3s[index] || row8s[index] == row4s[index] || row8s[index] == row5s[index] || row8s[index] == row6s[index] || row8s[index] == row7s[index] || row8s[index] == row9s[index] {
retval = false
}
if row9s[index] == row1s[index] || row9s[index] == row2s[index] || row9s[index] == row3s[index] || row9s[index] == row4s[index] || row9s[index] == row5s[index] || row9s[index] == row6s[index] || row9s[index] == row7s[index] || row9s[index] == row8s[index] {
retval = false
}
}
return retval
}
func render_combination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) string {
row1s := strconv.Itoa(row1)
row2s := strconv.Itoa(row2)
row3s := strconv.Itoa(row3)
row4s := strconv.Itoa(row4)
row5s := strconv.Itoa(row5)
row6s := strconv.Itoa(row6)
row7s := strconv.Itoa(row7)
row8s := strconv.Itoa(row8)
row9s := strconv.Itoa(row9)
2024-12-06 19:28:24 +01:00
2024-12-06 21:37:15 +01:00
// combination = row1s + "\n" + row2s + "\n" + row3s + "\n" + row4s + "\n" + row5s + "\n" + row6s + "\n" + row7s + "\n" + row8s + "\n" + row9s + "\n"
return row1s + "\n" + row2s + "\n" + row3s + "\n" + row4s + "\n" + row5s + "\n" + row6s + "\n" + row7s + "\n" + row8s + "\n" + row9s + "\n"
2024-12-06 19:28:24 +01:00
}
func find_blocks(blocks []int, row string) []int {
// Declare selection
var selection []int
2024-12-06 21:37:15 +01:00
var curr_blocks []int
2024-12-06 19:28:24 +01:00
2024-12-06 21:37:15 +01:00
// fmt.Println(row)
2024-12-06 19:28:24 +01:00
for letter := range row {
2024-12-06 21:37:15 +01:00
if len(selection) == 0 {
curr_blocks = blocks
} else {
curr_blocks = selection
selection = nil
2024-12-06 19:28:24 +01:00
}
2024-12-06 21:37:15 +01:00
// fmt.Println("Letter:", row[letter])
// fmt.Println("len(curr_blocks):", len(curr_blocks))
// fmt.Println("curr_blocks[0]:", curr_blocks[0])
for block := range curr_blocks {
curr_row := strconv.Itoa(curr_blocks[block])
if row[letter] == curr_row[letter] {
found_row, _ := strconv.Atoi(curr_row)
selection = append(selection, found_row)
}
if row[letter] == '0' {
found_row, _ := strconv.Atoi(curr_row)
selection = append(selection, found_row)
}
} // End for-loop
} // End for-loop
2024-12-06 19:28:24 +01:00
// Return selection
return selection
}
func load_blocks() []int {
var blocks []int
file, err := os.Open("blocks.csv")
if err != nil {
panic(err)
}
defer file.Close()
r := csv.NewReader(file)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
block, _ := strconv.Atoi(record[0])
blocks = append(blocks, block)
}
return blocks
2024-12-05 17:46:09 +01:00
}
func generate_blocks() []int {
var blocks []int
decvals := [9]int{49, 50, 51, 52, 53, 54, 55, 56, 57}
for counter := 123456789; counter <= 987654321; counter++ {
// Convert number to string ([]byte)
digits := strconv.Itoa(counter)
// Check if every number is only represented only once
var valid bool
valid = true
for decval := range decvals {
var count int
for digit := range digits {
if digits[digit] == byte(decvals[decval]) {
count = count + 1
}
}
if count != 1 {
valid = false
}
}
if valid {
blocks = append(blocks, counter)
}
}
return blocks
}
2024-12-06 19:28:24 +01:00
func print_block(block int) {
digits := strconv.Itoa(block)
fmt.Printf("%c %c %c\n%c %c %c\n%c %c %c\n\n", digits[0], digits[1], digits[2], digits[3], digits[4], digits[5], digits[6], digits[7], digits[8])
}