70 lines
1.1 KiB
Go
70 lines
1.1 KiB
Go
|
package solver
|
||
|
|
||
|
import (
|
||
|
"encoding/csv"
|
||
|
"io"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func (solver *Solver) load_blocks() {
|
||
|
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)
|
||
|
}
|
||
|
solver.blocks = blocks
|
||
|
}
|
||
|
|
||
|
func (solver *Solver) 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
|
||
|
|
||
|
}
|