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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
// A capset is a collection of cards that contains no sets.
// The supposed maximal cardinality of a capset in Set is 20.
// I wish to find such a collection with this program.
package main
import "log"
import "os"
import "fmt"
import "bufio"
import "errors"
var deck [81]string
var card2IntMap map[string]int
var useMap = true
func init() {
for i := range deck {
deck[i] = computeInt2Card(80 - i)
}
card2IntMap = make(map[string]int)
for i, v := range deck {
card2IntMap[v] = i
}
}
func computeInt2Card(num int) string {
if num < 0 {
log.Fatal("Number must be nonnegative.")
}
if num > 81 {
log.Fatal("Number must be less than 81.")
}
var strlist [4]byte
for i := 0; i < 4; i++ {
strlist[3-i] = '0' + byte(num%3)
num /= 3
}
return string(strlist[:])
}
func card2int(item string) int {
if useMap {
i, ok := card2IntMap[item]
if !ok {
return -1
}
return i
} else {
for i, v := range deck {
if v == item {
return i
}
}
return -1
}
}
type capset struct {
tableau []string
maxim int
hasSet bool
}
func (cs *capset) Scan(state fmt.ScanState, verb rune) error {
if verb != 'v' {
return errors.New("unexpected verb")
}
r, _, err := state.ReadRune()
if err != nil {
return err
}
if r != '[' {
return errors.New("didn't start with [")
}
cs.tableau = cs.tableau[:0]
for end := false; !end; {
token, err := state.Token(true, nil)
if err != nil {
return err
}
if len(token) == 0 {
return errors.New("Premature end")
}
if token[len(token)-1] == ']' {
end = true
token = token[:len(token)-1]
}
if len(token) > 0 {
card := string(token)
index := card2int(card)
if index == -1 {
return errors.New(fmt.Sprintf("Invalid card: %v", card))
}
// Always use same strings in hope underlying strcmp short circuits
card = deck[index]
cs.tableau = append(cs.tableau, card)
}
}
cs.updateTableauHasSetCached()
// Loop until it isn't a set, to make sure we didn't load a tableau with lots
// of sets.
for cs.hasSet {
cs.incrementTableau(true)
}
return nil
}
func (cs *capset) popTableau() string {
v := cs.tableau[len(cs.tableau)-1]
cs.tableau = cs.tableau[:len(cs.tableau)-1]
return v
}
func (cs *capset) incrementTableau(noAssume bool) bool {
pop := cs.hasSet
index := card2int(cs.tableau[len(cs.tableau)-1]) + 1
if index != len(deck) {
if pop {
cs.popTableau()
}
cs.tableau = append(cs.tableau, deck[index])
cs.updateTableauHasSet(noAssume)
return true
}
cs.popTableau()
index = card2int(cs.popTableau()) + 1
if len(cs.tableau) == 1 {
fmt.Println("incrementing second card to index", index)
if index+18 > len(deck) {
return false
}
}
cs.tableau = append(cs.tableau, deck[index])
cs.updateTableauHasSet(noAssume)
return true
}
func isSet(cards [3]string) bool {
for i := 0; i < 4; i++ {
if cards[0][i] == cards[1][i] && cards[0][i] == cards[2][i] {
continue
}
if cards[0][i] != cards[1][i] &&
cards[0][i] != cards[2][i] &&
cards[1][i] != cards[2][i] {
continue
}
return false
}
return true
}
func (cs *capset) updateTableauHasSet(noAssume bool) {
if noAssume {
cs.updateTableauHasSetCached()
} else {
cs.updateTableauHasSetOnlyNew()
}
}
// TODO(ejona): remove
func (cs *capset) updateTableauHasSetSimple() {
if len(cs.tableau) <= 2 {
cs.hasSet = false
return
}
var comb [3]string
for i := 0; i < len(cs.tableau); i++ {
comb[0] = cs.tableau[i]
for j := i + 1; j < len(cs.tableau); j++ {
comb[1] = cs.tableau[j]
for k := j + 1; k < len(cs.tableau); k++ {
comb[2] = cs.tableau[k]
if isSet(comb) {
cs.hasSet = true
return
}
}
}
}
cs.hasSet = false
}
func (cs *capset) updateTableauHasSetCached() {
var same [4]bool
for i := 0; i < len(cs.tableau); i++ {
for j := i + 1; j < len(cs.tableau); j++ {
for x := 0; x < 4; x++ {
same[x] = cs.tableau[i][x] == cs.tableau[j][x]
}
kloop:
for k := j + 1; k < len(cs.tableau); k++ {
for x := 0; x < 4; x++ {
if same[x] {
if cs.tableau[i][x] != cs.tableau[k][x] {
continue kloop
}
} else {
if cs.tableau[i][x] == cs.tableau[k][x] ||
cs.tableau[j][x] == cs.tableau[k][x] {
continue kloop
}
}
}
cs.hasSet = true
return
}
}
}
cs.hasSet = false
}
func (cs *capset) updateTableauHasSetOnlyNew() {
if len(cs.tableau) <= 2 {
cs.hasSet = false
return
}
// We try sets such that only the rightmost item could possibly be in a set.
i := len(cs.tableau) - 1
var same [4]bool
for j := 0; j < i; j++ {
for x := 0; x < 4; x++ {
same[x] = cs.tableau[i][x] == cs.tableau[j][x]
}
kloop:
for k := j + 1; k < i; k++ {
for x := 0; x < 4; x++ {
if same[x] {
if cs.tableau[i][x] != cs.tableau[k][x] {
continue kloop
}
} else {
if cs.tableau[i][x] == cs.tableau[k][x] ||
cs.tableau[j][x] == cs.tableau[k][x] {
continue kloop
}
}
}
cs.hasSet = true
return
}
}
cs.hasSet = false
}
func (cs *capset) findNextCapset() bool {
for cs.incrementTableau(false) {
if cs.hasSet {
continue
}
if len(cs.tableau) < cs.maxim {
continue
}
cs.maxim = len(cs.tableau)
return true
}
return false
}
func loadFromSave() *capset {
savefile, err := os.Open("./capset.out")
if err != nil {
return nil
}
defer savefile.Close()
scanner := bufio.NewScanner(savefile)
for scanner.Scan() {
}
var cs capset
_, err = fmt.Sscanln(scanner.Text(), &cs)
if err != nil {
return nil
}
return &cs
}
func main() {
cs := loadFromSave()
if cs == nil {
cs = &capset{tableau: []string{"2222"}}
}
cs.maxim = 20
fmt.Println("Starting processing with", cs.tableau)
outfile, err := os.OpenFile("./capset.out",
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
defer outfile.Close()
for cs.findNextCapset() {
fmt.Println(cs.tableau)
_, err = fmt.Fprintln(outfile, cs.tableau)
if err != nil {
log.Println(err)
}
err = outfile.Sync()
if err != nil {
log.Println(err)
}
}
}
|