Use strings instead of ints to store blocks (Closes #23)
This commit is contained in:
parent
647476f6d8
commit
2735074515
@ -3,7 +3,7 @@ package controller
|
||||
// Simple interface to store values shared amongst packages.
|
||||
type Controller struct {
|
||||
// All possible blocks/rows available
|
||||
Blocks []int
|
||||
Blocks []string
|
||||
// 1st row of the Sudoku puzzle.
|
||||
Row1 string
|
||||
// 2nd row of the Sudoku puzzle.
|
||||
|
@ -3,7 +3,6 @@ package solver
|
||||
import (
|
||||
"embed"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -19,7 +18,7 @@ func (solver *Solver) LoadBlocks() {
|
||||
defer solver.timeTrack(time.Now(), "Loaded blocks")
|
||||
log.Println("Loading blocks")
|
||||
|
||||
var blocks []int
|
||||
var blocks []string
|
||||
|
||||
file, err := f.ReadFile("blocks.csv")
|
||||
if err != nil {
|
||||
@ -29,8 +28,8 @@ func (solver *Solver) LoadBlocks() {
|
||||
temp := strings.Split(string(file), "\n")
|
||||
|
||||
for _, line := range temp {
|
||||
block, _ := strconv.Atoi(string(line))
|
||||
if block == 0 { // Ignore new-line at the end of the file.
|
||||
block := string(line)
|
||||
if block == "\n" || block == "" { // Ignore new-line at the end of the file.
|
||||
continue
|
||||
}
|
||||
blocks = append(blocks, block)
|
||||
|
@ -28,10 +28,10 @@ func (solver *Solver) PopulateBlocks() {
|
||||
}
|
||||
|
||||
// The actual function that finds the blocks matching the partial blocks.
|
||||
func (solver *Solver) findBlocks(row *string, rows *[]int) {
|
||||
func (solver *Solver) findBlocks(row *string, rows *[]string) {
|
||||
// Declare selection
|
||||
var selection []int
|
||||
var currBlocks []int
|
||||
var selection []string
|
||||
var currBlocks []string
|
||||
funcRow := *row
|
||||
|
||||
for letter := range funcRow {
|
||||
@ -45,14 +45,14 @@ func (solver *Solver) findBlocks(row *string, rows *[]int) {
|
||||
|
||||
for _, block := range currBlocks {
|
||||
|
||||
currRow := strconv.Itoa(block)
|
||||
currRow := block
|
||||
|
||||
if funcRow[letter] == currRow[letter] {
|
||||
foundRow, _ := strconv.Atoi(currRow)
|
||||
foundRow := currRow
|
||||
selection = append(selection, foundRow)
|
||||
}
|
||||
if funcRow[letter] == '0' {
|
||||
foundRow, _ := strconv.Atoi(currRow)
|
||||
foundRow := currRow
|
||||
selection = append(selection, foundRow)
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ func (solver *Solver) validator(rows1Index int, rows2Index int, rows3Index 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, solver.renderCombination(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]})
|
||||
}
|
||||
|
||||
}
|
||||
@ -194,53 +194,43 @@ func (solver *Solver) Tracker() {
|
||||
}
|
||||
|
||||
// Validate combination
|
||||
func (solver *Solver) validateCombination(row1 int, row2 int, row3 int, row4 int, row5 int, row6 int, row7 int, row8 int, row9 int) (retval bool) {
|
||||
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
|
||||
|
||||
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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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 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] {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -1,21 +0,0 @@
|
||||
package solver
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Prepare a valid solution for storage.
|
||||
func (solver *Solver) renderCombination(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)
|
||||
|
||||
return []string{row1s, row2s, row3s, row4s, row5s, row6s, row7s, row8s, row9s}
|
||||
}
|
@ -10,23 +10,23 @@ import (
|
||||
type Solver struct {
|
||||
Controller *controller.Controller
|
||||
// Slice of possible blocks for the 1st row.
|
||||
row1s []int
|
||||
row1s []string
|
||||
// Slice of possible blocks for the 2nd row.
|
||||
row2s []int
|
||||
row2s []string
|
||||
// Slice of possible blocks for the 3rd row.
|
||||
row3s []int
|
||||
row3s []string
|
||||
// Slice of possible blocks for the 4th row.
|
||||
row4s []int
|
||||
row4s []string
|
||||
// Slice of possible blocks for the 5th row.
|
||||
row5s []int
|
||||
row5s []string
|
||||
// Slice of possible blocks for the 6th row.
|
||||
row6s []int
|
||||
row6s []string
|
||||
// Slice of possible blocks for the 7th row.
|
||||
row7s []int
|
||||
row7s []string
|
||||
// Slice of possible blocks for the 8th row.
|
||||
row8s []int
|
||||
row8s []string
|
||||
// Slice of possible blocks for the 9th row.
|
||||
row9s []int
|
||||
row9s []string
|
||||
// Maximum number of possible solutions with the current set of rows.
|
||||
Iter int64
|
||||
// Progress counter, needs atomic due to the number of updates.
|
||||
|
Loading…
x
Reference in New Issue
Block a user