-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
einstein-riddle.go
472 lines (423 loc) · 9.27 KB
/
einstein-riddle.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package einsteinriddle
import (
"fmt"
"math/rand"
"os"
"strings"
"text/tabwriter"
"time"
)
type Generator struct {
Options Options
Vector []int
Pickeds []PickedGroup
Items map[int]ItemList
Kinds []int
}
type PickedGroup []int
type ItemList []string
type Item struct {
Kind int
Value string
Idx int
Person int
}
var BaseGenerator map[int]ItemList
const (
KindNationality = iota
KindHouseColor
KindPet
KindJob
KindBeverage
KindWeapon
KindTransport
KindRoom
)
var KindName map[int]string
var Kinds = []int{
KindNationality, KindHouseColor, KindPet, KindJob,
KindBeverage, KindWeapon, KindTransport, KindRoom,
}
func (il *ItemList) Shuffle() {
for i := range *il {
j := rand.Intn(i + 1)
(*il)[i], (*il)[j] = (*il)[j], (*il)[i]
}
}
func init() {
KindName = map[int]string{
KindNationality: "nationality",
KindHouseColor: "house-color",
KindPet: "pet",
KindJob: "job",
KindBeverage: "beverage",
KindWeapon: "weapon",
KindTransport: "transport",
KindRoom: "room",
}
BaseGenerator = map[int]ItemList{
KindNationality: {
"french",
"english",
"norvegian",
"american",
"portuguese",
"spannish",
"german",
"scottish",
},
KindHouseColor: {
"red",
"yellow",
"pink",
"orange",
"purple",
"magenta",
"blue",
"green",
},
KindPet: {
"dog",
"cat",
"horse",
"poney",
"fish",
"whale",
"beaver",
"bird",
"shark",
"snake",
},
KindJob: {
"teacher",
"architect",
"nurse",
"scientist",
"student",
"cop",
"designer",
"docker",
},
KindBeverage: {
"beer",
"wine",
"water",
"long-island-ice-tea",
"coca-cola",
"dr-pepper",
"blue-lagoon",
"fanta",
},
KindWeapon: {
"sword",
"gun",
"bazooka",
"grenade",
"bomb",
"assault-rifle",
"shotgun",
"knife",
"lasergun",
},
KindTransport: {
"bus",
"train",
"car",
"bike",
"plane",
"roller",
"motorbike",
"hoverboard",
},
KindRoom: {
"kitchen",
"bedroom",
"lobby",
"living-room",
"veranda",
"garden",
"pool",
"restroom",
"bathroom",
},
}
}
type Options struct {
Size int
Categories int
GroupSize int
Secrets int
Seed int64
SamePersonGroups int
}
func (o *Options) ApplyDefaults() {
if o.Size == 0 {
o.Size = 5
}
if o.Categories == 0 {
o.Categories = 5
}
if o.Secrets == 0 {
o.Secrets = 2
}
if o.GroupSize == 0 {
o.GroupSize = 2
}
if o.Seed == 0 {
o.Seed = time.Now().UnixNano()
}
if o.SamePersonGroups == 0 {
o.SamePersonGroups = o.Size * o.Categories / 3
}
}
func NewGenerator(options Options) *Generator {
options.ApplyDefaults()
rand.Seed(options.Seed)
generator := Generator{
Options: options,
Items: make(map[int]ItemList, 0),
Kinds: make([]int, options.Categories),
}
generator.Vector = make([]int, generator.Length())
generator.Pickeds = make([]PickedGroup, 0)
for i := range Kinds {
j := rand.Intn(i + 1)
Kinds[i], Kinds[j] = Kinds[j], Kinds[i]
}
for i := 0; i < options.Categories; i++ {
generator.Kinds[i] = Kinds[i]
}
for _, kind := range generator.Kinds {
itemList := BaseGenerator[kind]
itemList.Shuffle()
generator.Items[kind] = ItemList{}
for i := 0; i < options.Size; i++ {
generator.Items[kind] = append(generator.Items[kind], itemList[i])
}
}
return &generator
}
func (g *Generator) Show() {
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
defer w.Flush()
header := ""
for j := 0; j < g.Options.Size; j++ {
header += fmt.Sprintf("\t%d", j+1)
}
fmt.Fprintf(w, "%s\n", header)
for y, kind := range g.Kinds {
row := ""
for j := 0; j < g.Options.Size; j++ {
row += fmt.Sprintf("%d: %s\t", g.Vector[y*g.Options.Size+j], g.Items[kind][j])
}
fmt.Fprintf(w, "%s\t%s\n", KindName[kind], row)
}
fmt.Fprintln(w, "\n")
}
func (g *Generator) Length() int {
return g.Options.Size * g.Options.Categories
}
func (g *Generator) EOF() bool {
return len(g.Missings()) <= 2
}
func (g *Generator) PickSamePersonGroup(maxLevel int) []int {
picked := []int{}
// arbitrary choosen value to avoid infinite loops
for try := 0; try < 42; try++ {
person := rand.Intn(g.Options.Size)
kinds := make([]int, g.Options.Categories)
for j := 0; j < g.Options.Categories; j++ {
kinds[j] = j
}
for j := 0; j < g.Options.Categories; j++ {
k := rand.Intn(g.Options.Categories)
kinds[j], kinds[k] = kinds[k], kinds[j]
}
for _, kind := range kinds {
idx := kind*g.Options.Size + person
if g.Vector[idx] <= maxLevel {
picked = append(picked, idx)
if len(picked) == g.Options.GroupSize {
g.Pickeds = append(g.Pickeds, picked)
for j := 0; j < g.Options.GroupSize; j++ {
g.Vector[picked[j]]++
}
return picked
}
}
}
}
panic("should never happen")
return picked
}
func (g *Generator) PickAvailableGroup(maxLevel int) []int {
// FIXME: pick more groups of the same person
length := g.Length()
picked := make([]int, g.Options.GroupSize)
for j := 0; j < g.Options.GroupSize; j++ {
max := rand.Intn(length) + 1
k := 0
l := 0
idx := -1
for k < max {
for {
if g.Vector[(k+l)%length] == maxLevel-1 {
idx = (k + l) % length
break
}
l++
}
k++
}
picked[j] = idx
g.Vector[idx]++
}
g.Pickeds = append(g.Pickeds, picked)
return picked
}
func (g *Generator) At(idx int) Item {
item := Item{
Kind: g.Kinds[idx/g.Options.Size],
Idx: idx,
Person: idx % g.Options.Size,
}
item.Value = g.Items[item.Kind][item.Person]
return item
}
func (i *Item) Name() string {
return fmt.Sprintf("%s:%s", KindName[i.Kind], i.Value)
}
func (g *Generator) GroupString(group PickedGroup) string {
items := []Item{}
fullNames := []string{}
for _, idx := range group {
item := g.At(idx)
items = append(items, item)
fullNames = append(fullNames, item.Name())
}
if len(group) == 1 {
currentPerson := items[0].Person
if currentPerson == 0 {
return fmt.Sprintf("%s is on the far left", items[0].Name())
}
if currentPerson == g.Options.Size-1 {
return fmt.Sprintf("%s is on the far right", items[0].Name())
}
if g.Options.Size%2 == 1 && currentPerson == (g.Options.Size-1)/2 {
return fmt.Sprintf("%s is in the middle", items[0].Name())
}
panic("not implemented")
}
sameKind := true
samePerson := true
currentKind := items[0].Kind
currentPerson := items[0].Person
for j := 1; j < g.Options.GroupSize; j++ {
if currentKind != items[j].Kind {
sameKind = false
}
if currentPerson != items[j].Person {
samePerson = false
}
}
if samePerson {
return strings.Join(fullNames, " == ")
}
switch len(group) {
case 1:
panic("should never happen")
case 2:
// FIXME: sometimes just say "item0 is side by side with item1"
directLeft := items[0].Person == items[1].Person-1
directRight := items[0].Person == items[1].Person+1
if (directLeft || directRight) && rand.Intn(2) > 0 {
return fmt.Sprintf("%s is on the side of %s", items[0].Name(), items[1].Name())
}
if directLeft {
return fmt.Sprintf("%s is direct on the left of %s", items[0].Name(), items[1].Name())
}
if directRight {
return fmt.Sprintf("%s is direct on the right of %s", items[0].Name(), items[1].Name())
}
if items[0].Person < items[1].Person {
return fmt.Sprintf("%s is on the left of %s", items[0].Name(), items[1].Name())
}
if items[0].Person > items[1].Person {
return fmt.Sprintf("%s is on the right of %s", items[0].Name(), items[1].Name())
}
default:
panic("not implemented")
}
return fmt.Sprintf("%v %v %v", sameKind, samePerson, items)
}
func (g *Generator) Missings() []Item {
missings := []Item{}
for idx := 0; idx < g.Length(); idx++ {
if g.Vector[idx] == 0 {
missings = append(missings, g.At(idx))
}
}
return missings
}
func (g *Generator) PickItemAtExtremity(targetCounter int) []int {
picked := make([]int, 1)
idx := -1
// limits are arbitrary choosen to avoid infinite loop
// best effort for now
for ; targetCounter < 10; targetCounter++ {
for try := 0; try < 100; try++ {
direction := rand.Intn(3)
kind := rand.Intn(g.Options.Categories)
switch direction {
case 0:
idx = kind * g.Options.Size
case 1:
idx = kind*g.Options.Size + g.Options.Size - 1
case 2:
if g.Options.Size%2 == 1 {
idx = kind*g.Options.Size + (g.Options.Size-1)/2
} else {
continue
}
default:
panic("should never happen")
}
if g.Vector[idx] == targetCounter {
picked[0] = idx
g.Vector[idx]++
g.Pickeds = append(g.Pickeds, picked)
return picked
}
}
}
panic("should never happen")
return picked
}
func (g *Generator) Shazam() error {
for i := 0; i < g.Options.SamePersonGroups; i++ {
g.PickSamePersonGroup(0)
}
// pick at least each item one time
for len(g.Missings()) > g.Options.Secrets+1 {
g.PickAvailableGroup(1)
}
for len(g.Missings()) > g.Options.Secrets {
g.PickItemAtExtremity(0)
}
// pick again some items
for i := 0; i < 3; i++ {
g.PickAvailableGroup(2)
}
// pick groups of 1 item on an extremity
for i := 0; i < 3; i++ {
g.PickItemAtExtremity(1)
}
missingsKind := make(map[int]bool, 0)
for _, missing := range g.Missings() {
if missingsKind[missing.Kind] {
return fmt.Errorf("Invalid riddle: multiple missings item are from the same kind")
}
missingsKind[missing.Kind] = true
}
return nil
}