-
Notifications
You must be signed in to change notification settings - Fork 5
/
generation.go
148 lines (131 loc) · 3.54 KB
/
generation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package takuzu
import (
"errors"
"math"
"strconv"
"strings"
"github.com/cheatsnake/shadify/internal/helpers"
)
func generateField(size int) ([][]string, error) {
var (
index int
row string
)
field := []string{}
currentColumns := make([]string, size)
freeRows := generateRows(size)
for i := 0; i < size; i++ {
if i > 1 {
nextRowPattern := defineNextRow(currentColumns)
filteredRows := filteringRows(freeRows, nextRowPattern)
if len(filteredRows) < 1 {
return [][]string{}, errors.New(deadEnd)
}
index = helpers.GetRandomInteger(0, len(filteredRows)-1)
row = filteredRows[index]
index = helpers.IndexOf(row, freeRows)
} else {
index = helpers.GetRandomInteger(0, len(freeRows)-1)
row = freeRows[index]
}
field = append(field, row)
for j := 0; j < len(row); j++ {
currentColumns[j] += string(row[j])
}
if index == 0 {
freeRows = freeRows[1:]
} else if index == len(freeRows)-1 {
freeRows = freeRows[:len(freeRows)-1]
} else {
freeRows = append(freeRows[:index], freeRows[index+1:]...)
}
}
return splitField(field), nil
}
func generateRows(size int) []string {
rows := []string{}
max := math.Pow(2, float64(size))
for i := 0; i < int(max); i++ {
binary := strconv.FormatInt(int64(i), 2)
strBinary := helpers.PadStart(binary, size-len(binary), "0")
if strings.Contains(strBinary, strings.Repeat(takuzuZero, 3)) ||
strings.Contains(strBinary, strings.Repeat(takuzuOne, 3)) ||
strings.Count(strBinary, takuzuZero) > size/2 ||
strings.Count(strBinary, takuzuOne) > size/2 {
continue
}
rows = append(rows, strBinary)
}
return rows
}
func defineNextRow(columns []string) string {
size := len(columns)
nextRow := ""
for _, column := range columns {
if column[len(column)-2:] == strings.Repeat(takuzuZero, 2) {
nextRow += takuzuOne
} else if column[len(column)-2:] == strings.Repeat(takuzuOne, 2) {
nextRow += takuzuZero
} else if strings.Count(column, takuzuZero) == size/2 {
nextRow += takuzuOne
} else if strings.Count(column, takuzuOne) == size/2 {
nextRow += takuzuZero
} else if len(column) < size-2 && strings.Count(column, takuzuZero) == (size/2)-1 {
nextRow += takuzuOne
} else if len(column) < size-2 && strings.Count(column, takuzuOne) == (size/2)-1 {
nextRow += takuzuZero
} else {
nextRow += takuzuNull
}
}
return nextRow
}
func filteringRows(rows []string, pattern string) []string {
filteredRows := []string{}
for _, row := range rows {
isNextRow := true
for i := 0; i < len(row); i++ {
if string(pattern[i]) == takuzuNull {
continue
}
if row[i] != pattern[i] {
isNextRow = false
break
}
}
if isNextRow {
filteredRows = append(filteredRows, row)
}
}
return filteredRows
}
func splitField(field []string) [][]string {
splittedField := [][]string{}
for i, row := range field {
splittedRow := []string{}
for j := range row {
splittedRow = append(splittedRow, string(field[i][j]))
}
splittedField = append(splittedField, splittedRow)
}
return splittedField
}
func prepareField(field [][]string, fillFactor int) ([][]string, error) {
if len(field) == 0 {
return [][]string{}, errors.New(emptyField)
}
preparedField := [][]string{}
for i, row := range field {
preparedRow := []string{}
for j := range row {
randBool := helpers.GetRandomBool(fillFactor * 10)
if randBool {
preparedRow = append(preparedRow, string(field[i][j]))
} else {
preparedRow = append(preparedRow, takuzuNull)
}
}
preparedField = append(preparedField, preparedRow)
}
return preparedField, nil
}