sudoku-funpark/solver/processing.go

254 lines
8.7 KiB
Go
Raw Normal View History

package solver
import (
2025-01-21 21:43:27 +01:00
"log"
"strconv"
2025-01-23 20:26:43 +01:00
"time"
)
// Find all possible blocks that can be used to find a solution.
func (solver *Solver) PopulateBlocks() {
2025-01-23 20:26:43 +01:00
defer solver.timeTrack(time.Now(), "Populated blocks")
log.Println("Populating blocks")
solver.findBlocks(&solver.Controller.Row1, &solver.row1s)
solver.findBlocks(&solver.Controller.Row2, &solver.row2s)
solver.findBlocks(&solver.Controller.Row3, &solver.row3s)
solver.findBlocks(&solver.Controller.Row4, &solver.row4s)
solver.findBlocks(&solver.Controller.Row5, &solver.row5s)
solver.findBlocks(&solver.Controller.Row6, &solver.row6s)
solver.findBlocks(&solver.Controller.Row7, &solver.row7s)
solver.findBlocks(&solver.Controller.Row8, &solver.row8s)
solver.findBlocks(&solver.Controller.Row9, &solver.row9s)
// This calculates and stores the total number of solutions to validate.
solver.Iter = int64(len(solver.row1s)) * int64(len(solver.row2s)) * int64(len(solver.row3s)) * int64(len(solver.row4s)) * int64(len(solver.row5s)) * int64(len(solver.row6s)) * int64(len(solver.row7s)) * int64(len(solver.row8s)) * int64(len(solver.row9s))
2025-01-23 20:26:43 +01:00
2025-01-21 21:32:50 +01:00
}
// The actual function that finds the blocks matching the partial blocks.
func (solver *Solver) findBlocks(row *string, rows *[]string) {
// Declare selection
var selection []string
var currBlocks []string
funcRow := *row
for letter := range funcRow {
if len(selection) == 0 {
currBlocks = solver.Controller.Blocks
} else {
currBlocks = selection
selection = nil
}
for _, block := range currBlocks {
currRow := block
if funcRow[letter] == currRow[letter] {
foundRow := currRow
selection = append(selection, foundRow)
}
if funcRow[letter] == '0' {
foundRow := currRow
selection = append(selection, foundRow)
}
} // End for-loop
} // End for-loop
*rows = selection
}
// Iterate through all combination of blocks and validate them.
func (solver *Solver) CheckCombinations() {
for rows1Index := range solver.row1s {
for rows2Index := range solver.row2s {
for rows3Index := range solver.row3s {
for rows4Index := range solver.row4s {
for rows5Index := range solver.row5s {
for rows6Index := range solver.row6s {
for rows7Index := range solver.row7s {
for rows8Index := range solver.row8s {
for rows9Index := range solver.row9s {
go solver.validator(rows1Index, rows2Index, rows3Index, rows4Index, rows5Index, rows6Index, rows7Index, rows8Index, rows9Index)
2024-12-08 01:28:32 +01:00
}
}
}
}
}
}
}
}
}
}
// Validate the provided rows and verify it is a valid solution.
func (solver *Solver) validator(rows1Index int, rows2Index int, rows3Index int, rows4Index int, rows5Index int, rows6Index int, rows7Index int, rows8Index int, rows9Index int) {
solver.counter.Add(1)
if solver.validateCombination(solver.row1s[rows1Index], solver.row2s[rows2Index], solver.row3s[rows3Index], solver.row4s[rows4Index], solver.row5s[rows5Index], solver.row6s[rows6Index], solver.row7s[rows7Index], solver.row8s[rows8Index], solver.row9s[rows9Index]) {
solver.Controller.Solutions = append(solver.Controller.Solutions, []string{solver.row1s[rows1Index], solver.row2s[rows2Index], solver.row3s[rows3Index], solver.row4s[rows4Index], solver.row5s[rows5Index], solver.row6s[rows6Index], solver.row7s[rows7Index], solver.row8s[rows8Index], solver.row9s[rows9Index]})
}
2025-01-23 20:26:43 +01:00
}
// Keep track and output progress.
// Calculate rates, display percentages, estimate the ETA till completion.
func (solver *Solver) Tracker() {
2025-01-23 20:26:43 +01:00
// Add time tracking
2025-01-23 20:26:43 +01:00
defer solver.timeTrack(time.Now(), "Validated solutions")
log.Println("Validating solutions")
2025-01-25 17:32:11 +01:00
// Determine if the main-loop is done
var done bool
// Tracking progress in percentages
2025-01-23 20:26:43 +01:00
var percentage float32
2025-01-25 17:32:11 +01:00
// Tracking progress in validated solutions
2025-01-23 20:26:43 +01:00
var track int
2025-01-25 17:32:11 +01:00
// Tracking the rate, starting point
var rateStart int64
2025-01-25 17:32:11 +01:00
// Tracking the rate, difference between previous iterations
var rateDiff int64
2025-01-23 20:26:43 +01:00
// Tracking duration
var timerStart = time.Now()
2025-01-23 20:26:43 +01:00
// Estimation how long it will take
var est_fin string
// While not needed for rateDiff anymore, it makes estimation calculations more accurate. ☹️
time.Sleep(time.Second)
// for solver.Iter != solver.counter { // Start for-loop
2025-01-25 17:32:11 +01:00
for !done {
2025-01-23 20:26:43 +01:00
// Determine how far we are.
percentage = (float32(solver.counter.Load()) / (float32(solver.Iter) / 100))
2025-01-25 17:32:11 +01:00
// Reset the loop
rateDiff = solver.counter.Load() - rateStart
2025-01-25 17:32:11 +01:00
if track <= int(percentage) || rateDiff == 0 { // Start if-statement
2025-01-23 20:26:43 +01:00
// Make sure something happened, making rateStart the only reliable variable
if solver.Iter == solver.counter.Load() {
2025-01-23 20:26:43 +01:00
percentage = 100
solver.counter.Store(solver.Iter)
2025-01-25 17:32:11 +01:00
done = true
2025-01-23 20:26:43 +01:00
}
timer_elapsed := time.Since(timerStart)
solver.rates = append(solver.rates, rateDiff)
rate_avg := solver.calcAVG()
2025-01-23 20:26:43 +01:00
2025-01-25 14:34:51 +01:00
// Estimate when this is finished
if rateDiff == 0 {
2025-01-23 20:26:43 +01:00
est_fin = "N/A"
} else {
duration_int := (solver.Iter - solver.counter.Load()) / rate_avg
duration_string := strconv.Itoa(int(duration_int)) + "s"
est, err := time.ParseDuration(duration_string)
if err != nil {
est_fin = "parse error"
} else {
est_fin = est.String()
}
2025-01-23 20:26:43 +01:00
}
2025-01-25 17:32:11 +01:00
// Printing the progress
log.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.FormatInt(solver.counter.Load(), 10) + "/" + strconv.Itoa(int(solver.Iter)) + "); Rate: " + strconv.FormatInt(rateDiff, 10) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
2025-01-23 20:26:43 +01:00
2025-01-25 17:32:11 +01:00
// After we are done printing, exit this for-loop
if percentage == 100 {
break
}
2025-01-23 20:26:43 +01:00
// Wrap up the loop or break
if int(percentage) > track {
track = int(percentage)
} else {
track = track + 1
}
timerStart = time.Now()
2025-01-23 20:26:43 +01:00
}
2025-01-25 17:32:11 +01:00
// Resert the rate counter
rateStart = solver.counter.Load()
2025-01-25 17:32:11 +01:00
// Sleep for a second
if solver.Iter != solver.counter.Load() {
time.Sleep(1 * time.Second)
}
2025-01-25 17:32:11 +01:00
} // End for-loop
}
// Validate combination
func (solver *Solver) validateCombination(row1 string, row2 string, row3 string, row4 string, row5 string, row6 string, row7 string, row8 string, row9 string) (retval bool) {
retval = true
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] == row1[index] || 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] == row1[index] || row3[index] == row2[index] || 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] == row1[index] || row4[index] == row2[index] || row4[index] == row3[index] || 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] == row1[index] || row5[index] == row2[index] || row5[index] == row3[index] || row5[index] == row4[index] || row5[index] == row6[index] || row5[index] == row7[index] || row5[index] == row8[index] || row5[index] == row9[index] {
retval = false
}
if row6[index] == row1[index] || row6[index] == row2[index] || row6[index] == row3[index] || row6[index] == row4[index] || row6[index] == row5[index] || row6[index] == row7[index] || row6[index] == row8[index] || row6[index] == row9[index] {
retval = false
}
if row7[index] == row1[index] || row7[index] == row2[index] || row7[index] == row3[index] || row7[index] == row4[index] || row5[index] == row6[index] || row7[index] == row6[index] || row7[index] == row8[index] || row7[index] == row9[index] {
retval = false
}
if row8[index] == row1[index] || row8[index] == row2[index] || row8[index] == row3[index] || row8[index] == row4[index] || row8[index] == row5[index] || row8[index] == row6[index] || row8[index] == row7[index] || row8[index] == row9[index] {
retval = false
}
if row9[index] == row1[index] || row9[index] == row2[index] || row9[index] == row3[index] || row9[index] == row4[index] || row9[index] == row5[index] || row9[index] == row6[index] || row9[index] == row7[index] || row9[index] == row8[index] {
retval = false
}
}
return retval
}
2025-01-25 14:34:51 +01:00
// Calculate the average rate in a stored slice of rates.
func (solver *Solver) calcAVG() (avg int64) {
var avgSum int64
2025-01-25 14:34:51 +01:00
for _, value := range solver.rates {
avgSum += value
2025-01-25 14:34:51 +01:00
}
avg = avgSum / int64(len(solver.rates))
2025-01-25 14:34:51 +01:00
return
2025-01-25 14:34:51 +01:00
}