Refactor Sudoku solver architecture
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.kleiax.de/homepage/field"
|
||||
)
|
||||
|
||||
// https://github.com/grantm/sudoku-exchange-puzzle-bank/tree/master
|
||||
type PuzzleBank struct {
|
||||
ParserHelper
|
||||
}
|
||||
|
||||
func (pb *PuzzleBank) Parse(data []byte) error {
|
||||
|
||||
lines := bytes.Split(data, []byte("\n"))
|
||||
for _, line := range lines {
|
||||
if len(line) == 0 { // Überspringe leere Zeile
|
||||
continue
|
||||
}
|
||||
|
||||
if len(line) != 99 {
|
||||
return fmt.Errorf("Zeile hat die falsche länge. soll: 100, ist: %d", len(line))
|
||||
}
|
||||
|
||||
//siehe Readme in github repo
|
||||
//sha1Hash := bytes.TrimSpace(line[0:12]) // Wird aktuell nicht gebraucht, später zum sudoku vergleichen
|
||||
sudokuStr := string(line[13:94])
|
||||
ratingStr := string(line[96:99])
|
||||
//fmt.Println(sudokuStr)
|
||||
|
||||
var rating float64
|
||||
_, err := fmt.Sscanf(ratingStr, "%f", &rating)
|
||||
if err != nil {
|
||||
return errors.New("can nor parse raiting")
|
||||
}
|
||||
|
||||
props := field.Properties{
|
||||
Rows: 9,
|
||||
Columns: 9,
|
||||
BlockRows: 3,
|
||||
BlockColumns: 3,
|
||||
BlockSizeRow: 3,
|
||||
BlockSizeColumn: 3,
|
||||
Rating: rating,
|
||||
}
|
||||
|
||||
cells := make([][]field.Cell, props.Rows)
|
||||
for i := range cells {
|
||||
cells[i] = make([]field.Cell, props.Columns)
|
||||
for j := range cells[i] {
|
||||
number, err := strconv.Atoi(string(sudokuStr[i*9+j]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Kann Zeichen nicht in Zahl konvertieren")
|
||||
}
|
||||
pos := field.NewPosition(
|
||||
i,
|
||||
j,
|
||||
i/props.BlockRows,
|
||||
j/props.BlockColumns,
|
||||
i%props.BlockRows,
|
||||
j%props.BlockColumns)
|
||||
|
||||
cells[i][j] = *field.NewCell(number, pos)
|
||||
}
|
||||
}
|
||||
pb.fields = append(pb.fields, *field.New(props, cells))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user