-
Notifications
You must be signed in to change notification settings - Fork 7
/
reader.go
84 lines (72 loc) · 1.48 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"io/ioutil"
"strings"
"fmt"
)
func ReadFile(filename string) (Puzzle, error) {
dat, err := ioutil.ReadFile(filename)
if err != nil {
return Puzzle{}, err
}
return createPuzzle(string(dat[:]))
}
func createPuzzle(model string) (Puzzle, error) {
rows := strings.Split(strings.Trim(strings.ToUpper(model), " \t\n\r"), "\n")
var maxLen int8 = 0
var grid = make(Grid, len(rows), len(rows[0]))
for index, row := range rows {
if len(row) == 0 {
continue
}
var rowValues = []uint8{}
var counter int8 = 0
for _, char := range row {
val := int8(char)
if val == 32 {
continue
}
counter ++
if val > 48 && val <= 57 {
rowValues = append(rowValues, uint8(val-48))
} else if val >= 65 && val <= 90 {
rowValues = append(rowValues, uint8(val-55))
} else {
return Puzzle{}, fmt.Errorf("only numbers [1-9] and latin characters allowed in model. Loaded model [%s].", filename)
}
}
if maxLen < counter {
maxLen = counter
}
grid[index] = rowValues
}
pieces := GetPiecesFromGrid(grid)
var solutions []Grid
var puzzle = Puzzle{
pieces,
copyGrid(grid[:]),
grid[:],
max(int8(len(rows)), maxLen),
minPieceSize(pieces),
&solutions,
false,
false,
&WinInfo{},
}
return puzzle, nil
}
func max(a int8, b int8) int8 {
if a >= b {
return a
}
return b
}
func minPieceSize(pieces []Piece) int {
var min int = 255
for _, piece := range pieces {
if piece.Size < min {
min = piece.Size
}
}
return min
}