Compare commits
8 Commits
5d02036f9e
...
v20250126
Author | SHA1 | Date | |
---|---|---|---|
74f1fb63e1 | |||
a7feb7101a | |||
28ecc74d7a | |||
6d6db0ed28 | |||
53953cf47c | |||
acf6ad1bb9 | |||
26b78420a2 | |||
582e268d56 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
builds
|
32
README.md
32
README.md
@ -1,5 +1,5 @@
|
||||
# Sudoku Funpark
|
||||
Creating worlds most inefficient Sudoku solver, by trying every option, without any smart approach.
|
||||
Creating worlds most inefficient Sudoku solver, by trying every option, without any smart approach.
|
||||
|
||||
_(This was a learning project to get a better grasp of Golang. But more important; It was fun to do!.)_
|
||||
|
||||
@ -54,23 +54,23 @@ The outpot (of the short running parameters) will look something like this:
|
||||
2025/01/24 00:05:58 Populated blocks (438.73054ms)
|
||||
2025/01/24 00:05:58 Number of (potential) solutions: 26542080
|
||||
2025/01/24 00:05:58 Validating solutions
|
||||
2025/01/24 00:05:59 Processing: 6% (1729332/26542080); Rate (avg): 1729330/sec for 1.000003166s; Time left (est.): 14 seconds
|
||||
2025/01/24 00:06:00 Processing: 13% (3461753/26542080); Rate (avg): 1732418/sec for 1.000002285s; Time left (est.): 13 seconds
|
||||
2025/01/24 00:06:01 Processing: 19% (5228965/26542080); Rate (avg): 1767215/sec for 1.000019297s; Time left (est.): 12 seconds
|
||||
2025/01/24 00:06:02 Processing: 26% (6996958/26542080); Rate (avg): 1767992/sec for 1.000200176s; Time left (est.): 11 seconds
|
||||
2025/01/24 00:06:03 Processing: 33% (8767450/26542080); Rate (avg): 1770495/sec for 1.000016352s; Time left (est.): 10 seconds
|
||||
2025/01/24 00:06:04 Processing: 39% (10576900/26542080); Rate (avg): 1809450/sec for 1.000014638s; Time left (est.): 8 seconds
|
||||
2025/01/24 00:06:05 Processing: 46% (12400058/26542080); Rate (avg): 1823158/sec for 1.000352862s; Time left (est.): 7 seconds
|
||||
2025/01/24 00:06:06 Processing: 53% (14185155/26542080); Rate (avg): 1785095/sec for 1.000254888s; Time left (est.): 6 seconds
|
||||
2025/01/24 00:06:07 Processing: 60% (15968402/26542080); Rate (avg): 1783245/sec for 1.000002305s; Time left (est.): 5 seconds
|
||||
2025/01/24 00:06:08 Processing: 66% (17655770/26542080); Rate (avg): 1687370/sec for 1.000068309s; Time left (est.): 5 seconds
|
||||
2025/01/24 00:06:09 Processing: 73% (19442885/26542080); Rate (avg): 1787111/sec for 1.000006984s; Time left (est.): 3 seconds
|
||||
2025/01/24 00:06:10 Processing: 79% (21183545/26542080); Rate (avg): 1740661/sec for 1.000002395s; Time left (est.): 3 seconds
|
||||
2025/01/24 00:06:11 Processing: 86% (22998945/26542080); Rate (avg): 1815402/sec for 1.000113534s; Time left (est.): 1 second
|
||||
2025/01/24 00:06:12 Processing: 90% (24109203/26542080); Rate (avg): 1110261/sec for 1.000312346s; Time left (est.): 2 seconds
|
||||
2025/01/24 00:05:59 Processing: 6% (1729332/26542080); Rate (avg): 1729330/sec for 1.000003166s; Time left (est.): 14 seconds
|
||||
2025/01/24 00:06:00 Processing: 13% (3461753/26542080); Rate (avg): 1732418/sec for 1.000002285s; Time left (est.): 13 seconds
|
||||
2025/01/24 00:06:01 Processing: 19% (5228965/26542080); Rate (avg): 1767215/sec for 1.000019297s; Time left (est.): 12 seconds
|
||||
2025/01/24 00:06:02 Processing: 26% (6996958/26542080); Rate (avg): 1767992/sec for 1.000200176s; Time left (est.): 11 seconds
|
||||
2025/01/24 00:06:03 Processing: 33% (8767450/26542080); Rate (avg): 1770495/sec for 1.000016352s; Time left (est.): 10 seconds
|
||||
2025/01/24 00:06:04 Processing: 39% (10576900/26542080); Rate (avg): 1809450/sec for 1.000014638s; Time left (est.): 8 seconds
|
||||
2025/01/24 00:06:05 Processing: 46% (12400058/26542080); Rate (avg): 1823158/sec for 1.000352862s; Time left (est.): 7 seconds
|
||||
2025/01/24 00:06:06 Processing: 53% (14185155/26542080); Rate (avg): 1785095/sec for 1.000254888s; Time left (est.): 6 seconds
|
||||
2025/01/24 00:06:07 Processing: 60% (15968402/26542080); Rate (avg): 1783245/sec for 1.000002305s; Time left (est.): 5 seconds
|
||||
2025/01/24 00:06:08 Processing: 66% (17655770/26542080); Rate (avg): 1687370/sec for 1.000068309s; Time left (est.): 5 seconds
|
||||
2025/01/24 00:06:09 Processing: 73% (19442885/26542080); Rate (avg): 1787111/sec for 1.000006984s; Time left (est.): 3 seconds
|
||||
2025/01/24 00:06:10 Processing: 79% (21183545/26542080); Rate (avg): 1740661/sec for 1.000002395s; Time left (est.): 3 seconds
|
||||
2025/01/24 00:06:11 Processing: 86% (22998945/26542080); Rate (avg): 1815402/sec for 1.000113534s; Time left (est.): 1 second
|
||||
2025/01/24 00:06:12 Processing: 90% (24109203/26542080); Rate (avg): 1110261/sec for 1.000312346s; Time left (est.): 2 seconds
|
||||
2025/01/24 00:06:13 Processing: 100% (26542080/26542080); Rate (avg): 0/sec for 1.000117421s; Time left (est.): N/A
|
||||
2025/01/24 00:06:13 Validated solutions (15.002654066s)
|
||||
2025/01/24 00:06:13
|
||||
2025/01/24 00:06:13
|
||||
Solution #1:
|
||||
769154832
|
||||
154832769
|
||||
|
10
Taskfile.yml
10
Taskfile.yml
@ -2,6 +2,10 @@
|
||||
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
APP: sudoku-funpark
|
||||
BUILD_DIR: builds
|
||||
|
||||
tasks:
|
||||
default:
|
||||
cmds:
|
||||
@ -23,3 +27,9 @@ tasks:
|
||||
cmds:
|
||||
- golangci-lint run
|
||||
silent: true
|
||||
build:
|
||||
cmds:
|
||||
- mkdir -p {{.BUILD_DIR}}
|
||||
- rm {{.BUILD_DIR}}/* || true
|
||||
- go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o {{.BUILD_DIR}}/{{.APP}}.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
|
||||
- for i in `ls {{.BUILD_DIR}}/*windows*`; do mv -v $i $i.exe; done
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (solver *Solver) parse_flags() {
|
||||
@ -19,6 +20,8 @@ func (solver *Solver) parse_flags() {
|
||||
var row7 string
|
||||
var row8 string
|
||||
var row9 string
|
||||
var split int
|
||||
var part int
|
||||
|
||||
// Define parameters
|
||||
flag.StringVar(&row1, "row1", "000000000", "1st row of the sudoku puzzle.")
|
||||
@ -30,10 +33,25 @@ func (solver *Solver) parse_flags() {
|
||||
flag.StringVar(&row7, "row7", "000000000", "7th row of the sudoku puzzle.")
|
||||
flag.StringVar(&row8, "row8", "000000000", "8th row of the sudoku puzzle.")
|
||||
flag.StringVar(&row9, "row9", "000000000", "9th row of the sudoku puzzle.")
|
||||
flag.IntVar(&solver.numcpus, "numcpu", runtime.NumCPU(), "Number of CPU cores to assign to this task.")
|
||||
flag.IntVar(&split, "split", 1, "Split the tasks in n parts. This depends on the availability of the first row.")
|
||||
flag.IntVar(&part, "part", 1, "Process part x in n parts. Cannot be lower than 1, or higher than specified in split.")
|
||||
|
||||
// Parse the flags
|
||||
flag.Parse()
|
||||
|
||||
// Process any changes to the CPU usage.
|
||||
if solver.numcpus <= 0 {
|
||||
log.Printf("ERROR: Number of CPU cores must be 1 or higher.\n\n")
|
||||
solver.print_Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if solver.numcpus != runtime.NumCPU() {
|
||||
runtime.GOMAXPROCS(solver.numcpus)
|
||||
}
|
||||
|
||||
// Process rows
|
||||
if row1 == "000000000" || row2 == "000000000" || row3 == "000000000" || row4 == "000000000" || row5 == "000000000" || row6 == "000000000" || row7 == "000000000" || row8 == "000000000" || row9 == "000000000" {
|
||||
log.Printf("ERROR: All parameters must be entered.\n\n")
|
||||
solver.print_Usage()
|
||||
@ -62,6 +80,24 @@ func (solver *Solver) parse_flags() {
|
||||
solver.row8 = row8
|
||||
solver.row9 = row9
|
||||
|
||||
// Process workload splitting
|
||||
// Ensure split and part are 1 or higher
|
||||
if split <= 0 || part <= 0 {
|
||||
log.Printf("ERROR: '-split' and '-part' need to be 1 or higher.\n")
|
||||
solver.print_Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Ensure part is between 1 and split
|
||||
if part > split {
|
||||
log.Printf("ERROR: '-part' cannot be bigger than `-split`.\n")
|
||||
solver.print_Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
solver.split = split
|
||||
solver.part = part
|
||||
|
||||
}
|
||||
|
||||
func (solver *Solver) validate_row(name string, row string) {
|
||||
|
@ -85,7 +85,8 @@ func (solver *Solver) check_combinations() {
|
||||
|
||||
func (solver *Solver) routine_validator(rows1_index int, rows2_index int, rows3_index int, rows4_index int, rows5_index int, rows6_index int, rows7_index int, rows8_index int, rows9_index int) {
|
||||
|
||||
solver.counter = solver.counter + 1
|
||||
// solver.counter = solver.counter + 1
|
||||
solver.counter.Add(1)
|
||||
|
||||
if solver.validate_combination(solver.row1s[rows1_index], solver.row2s[rows2_index], solver.row3s[rows3_index], solver.row4s[rows4_index], solver.row5s[rows5_index], solver.row6s[rows6_index], solver.row7s[rows7_index], solver.row8s[rows8_index], solver.row9s[rows9_index]) {
|
||||
solver.solutions = append(solver.solutions, solver.render_combination(solver.row1s[rows1_index], solver.row2s[rows2_index], solver.row3s[rows3_index], solver.row4s[rows4_index], solver.row5s[rows5_index], solver.row6s[rows6_index], solver.row7s[rows7_index], solver.row8s[rows8_index], solver.row9s[rows9_index]))
|
||||
@ -98,51 +99,63 @@ func (solver *Solver) tracker() {
|
||||
defer solver.timeTrack(time.Now(), "Validated solutions")
|
||||
log.Println("Validating solutions")
|
||||
|
||||
// Tracking percenting an progress
|
||||
// Determine if the main-loop is done
|
||||
var done bool
|
||||
|
||||
// Tracking progress in percentages
|
||||
var percentage float32
|
||||
// Tracking progress in validated solutions
|
||||
var track int
|
||||
|
||||
// Tracking the rate
|
||||
var rate_start int
|
||||
var rate_stop int
|
||||
var rate_diff int
|
||||
// Tracking the rate, starting point
|
||||
var rate_start int64
|
||||
// Tracking the rate, difference between previous iterations
|
||||
var rate_diff int64
|
||||
|
||||
// Tracking duration
|
||||
var timer_start = time.Now()
|
||||
// Prevent division-by-zero error when establishing `rate`
|
||||
// Prevent division-by-zero error when establishing `rate_diff`
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// Estimation how long it will take
|
||||
var est_fin string
|
||||
|
||||
for solver.iter != solver.counter {
|
||||
// for solver.iter != solver.counter { // Start for-loop
|
||||
for !done {
|
||||
|
||||
// Determine how far we are.
|
||||
percentage = (float32(solver.counter) / (float32(solver.iter) / 100))
|
||||
if track <= int(percentage) {
|
||||
// Reset the loop
|
||||
rate_stop = int(solver.counter)
|
||||
rate_diff = rate_stop - rate_start
|
||||
percentage = (float32(solver.counter.Load()) / (float32(solver.iter) / 100))
|
||||
|
||||
// Reset the loop
|
||||
rate_diff = solver.counter.Load() - rate_start
|
||||
|
||||
if track <= int(percentage) || rate_diff == 0 { // Start if-statement
|
||||
|
||||
// Make sure something happened, making rate_start the only reliable variable
|
||||
if rate_diff == 0 && rate_start > 1 {
|
||||
if rate_diff == 0 {
|
||||
percentage = 100
|
||||
solver.counter = solver.iter
|
||||
solver.counter.Store(solver.iter)
|
||||
done = true
|
||||
}
|
||||
|
||||
timer_elapsed := time.Since(timer_start)
|
||||
rate := int64(rate_diff) / int64(timer_elapsed.Seconds())
|
||||
solver.rates = append(solver.rates, rate_diff)
|
||||
rate_avg := solver.calc_avg()
|
||||
|
||||
// Estimate when this is finished:
|
||||
// TODO: Make this Bayesian
|
||||
// Estimate when this is finished
|
||||
if rate_diff == 0 {
|
||||
est_fin = "N/A"
|
||||
} else {
|
||||
est_fin = solver.secondsToHuman((int(solver.iter) - int(solver.counter)) / int(rate))
|
||||
est_fin = solver.secondsToHuman((solver.iter - solver.counter.Load()) / rate_avg)
|
||||
}
|
||||
|
||||
// Printing the meat
|
||||
log.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.Itoa(int(solver.counter)) + "/" + strconv.Itoa(int(solver.iter)) + "); Rate (avg): " + strconv.Itoa(int(rate)) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
|
||||
// Printing the progress
|
||||
log.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.FormatInt(solver.counter.Load(), 10) + "/" + strconv.Itoa(int(solver.iter)) + "); Rate: " + strconv.FormatInt(rate_diff, 10) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
|
||||
|
||||
// After we are done printing, exit this for-loop
|
||||
if percentage == 100 {
|
||||
break
|
||||
}
|
||||
|
||||
// Wrap up the loop or break
|
||||
if int(percentage) > track {
|
||||
@ -150,17 +163,18 @@ func (solver *Solver) tracker() {
|
||||
} else {
|
||||
track = track + 1
|
||||
}
|
||||
rate_start = rate_stop
|
||||
|
||||
timer_start = time.Now()
|
||||
|
||||
if rate_diff == 0 && rate_start > 100 {
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Resert the rate counter
|
||||
rate_start = solver.counter.Load()
|
||||
|
||||
// Sleep for a second
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
}
|
||||
} // End for-loop
|
||||
|
||||
}
|
||||
|
||||
@ -219,3 +233,15 @@ func (solver *Solver) validate_combination(row1 int, row2 int, row3 int, row4 in
|
||||
|
||||
return retval
|
||||
}
|
||||
|
||||
func (solver *Solver) calc_avg() (avg int64) {
|
||||
var avg_sum int64
|
||||
|
||||
for _, value := range solver.rates {
|
||||
avg_sum += value
|
||||
}
|
||||
|
||||
avg = avg_sum / int64(len(solver.rates))
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package solver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
@ -11,12 +13,23 @@ func Run() {
|
||||
// Parse and handle flags
|
||||
solver.parse_flags()
|
||||
|
||||
// Report number of CPUs being used, if set.
|
||||
if runtime.NumCPU() != solver.numcpus {
|
||||
log.Println("Using " + strconv.Itoa(solver.numcpus) + " CPUs, (was " + strconv.Itoa(runtime.NumCPU()) + ")")
|
||||
}
|
||||
|
||||
// Load blocks from CSV file
|
||||
solver.load_blocks()
|
||||
|
||||
// Find rows that fit with the entered rows
|
||||
solver.populate_blocks()
|
||||
|
||||
// If needed, split the workload
|
||||
// May exit and throw an error if the work load isn't viable
|
||||
if solver.split != 1 {
|
||||
solver.select_workload()
|
||||
}
|
||||
|
||||
// Print the total number of solutions to validate
|
||||
log.Println("Number of (potential) solutions:", solver.iter)
|
||||
|
||||
|
61
solver/split.go
Normal file
61
solver/split.go
Normal file
@ -0,0 +1,61 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Perform some checks
|
||||
// and
|
||||
// Modify solver.row1s so it limits the workload to what is only desired.
|
||||
func (solver *Solver) select_workload() {
|
||||
if solver.split > len(solver.row1s) {
|
||||
log.Println("ERROR: Unable to divide the workload in " + strconv.Itoa(solver.split) + " parts, when only " + strconv.Itoa(len(solver.row1s)) + " are available.\n\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer solver.timeTrack(time.Now(), "Workload set")
|
||||
log.Println("Setting workload")
|
||||
log.Println("We are agent " + strconv.Itoa(solver.part) + " of " + strconv.Itoa(solver.split))
|
||||
workloads := solver.split_workload()
|
||||
solver.set_workload(workloads)
|
||||
}
|
||||
|
||||
// Determine how workload should be split among the agents
|
||||
func (solver *Solver) split_workload() []int {
|
||||
agents := make([]int, solver.split)
|
||||
var tracker int
|
||||
var tasks int = len(solver.row1s)
|
||||
|
||||
for tasks != 0 {
|
||||
agents[tracker] += 1
|
||||
tasks -= 1
|
||||
tracker += 1
|
||||
if tracker == solver.split {
|
||||
tracker = 0
|
||||
}
|
||||
}
|
||||
|
||||
return agents
|
||||
}
|
||||
|
||||
// Set the workload by setting solver.row1s
|
||||
func (solver *Solver) set_workload(agents []int) {
|
||||
var start int = 0
|
||||
var finish int = 0
|
||||
for key, value := range agents {
|
||||
if key == solver.part-1 {
|
||||
finish = start + value
|
||||
break
|
||||
} else {
|
||||
start += value
|
||||
}
|
||||
}
|
||||
|
||||
// Set the shortened set of instructions
|
||||
solver.row1s = solver.row1s[start:finish]
|
||||
|
||||
// Recalculate how much we need to grind through
|
||||
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))
|
||||
}
|
@ -22,7 +22,7 @@ func (solver *Solver) plural(count int, singular string) (result string) {
|
||||
return
|
||||
}
|
||||
|
||||
func (solver *Solver) secondsToHuman(input int) (result string) {
|
||||
func (solver *Solver) secondsToHuman(input int64) (result string) {
|
||||
years := math.Floor(float64(input) / 60 / 60 / 24 / 7 / 30 / 12)
|
||||
seconds := input % (60 * 60 * 24 * 7 * 30 * 12)
|
||||
months := math.Floor(float64(seconds) / 60 / 60 / 24 / 7 / 30)
|
||||
|
@ -1,5 +1,9 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type Solver struct {
|
||||
blocks []int
|
||||
row1 string
|
||||
@ -21,6 +25,10 @@ type Solver struct {
|
||||
row8s []int
|
||||
row9s []int
|
||||
iter int64
|
||||
counter int64
|
||||
counter atomic.Int64
|
||||
solutions []string
|
||||
rates []int64
|
||||
numcpus int
|
||||
split int
|
||||
part int
|
||||
}
|
||||
|
Reference in New Issue
Block a user