-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.go
440 lines (392 loc) · 7.45 KB
/
cube.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
package cube
import (
"crypto/rand"
"fmt"
"strings"
)
// Cube represents a standard rubik's cube
type Cube [NumSides][NumTiles]uint8
// NumSides is the number of sides of a cube
const NumSides = 6
// NumTiles is the number of mobile tiles on a Rubik's Cube
const NumTiles = 8
/* An enumeration of the edges of a Rubic's Cube */
const (
Front = iota + 1
Top
Right
Back
Bottom
Left
)
// Solution is the sequence of moves to solve a Rubik's Cube
type Solution []uint8
// Solver A method for solving a Rubik's Cube
type Solver func(Cube) Solution
// NewCube initilizes a solved Cube
func NewCube() Cube {
var c Cube
for s := range c {
for i := range c[s] {
c[s][i] = uint8(s)
}
}
return c
}
func (c *Cube) String() string {
var str [4 * 3]string
// 1
// 5 0 2
// 4
// 3
// Space for each row but 5's
for i := range str {
if i >= 3 && i <= 5 {
continue
}
str[i] += " "
}
for _, i := range []int{5, 1, 0, 2, 4, 3} {
var offset int
switch i {
case 1:
offset = 0
case 5:
fallthrough
case 0:
fallthrough
case 2:
offset = 3
case 4:
offset = 6
case 3:
offset = 9
}
if i < 3 {
str[0+offset] += fmt.Sprint(c[i][0], c[i][1], c[i][2], " ")
str[1+offset] += fmt.Sprint(c[i][7], " ", c[i][3], " ")
str[2+offset] += fmt.Sprint(c[i][6], c[i][5], c[i][4], " ")
} else {
str[0+offset] += fmt.Sprint(c[i][4], c[i][5], c[i][6], " ")
str[1+offset] += fmt.Sprint(c[i][3], " ", c[i][7], " ")
str[2+offset] += fmt.Sprint(c[i][2], c[i][1], c[i][0], " ")
}
}
return strings.Join(str[:], "\n")
}
// front, right, top, back, left, bottom
// front: top, right, left, bottom :back
// left: front, top, back, bottom :right
// top: front, right, left, back :bottom
// <-> right <-> back <-> left <-> front
// <-> front <-> bottom <-> back <-> top
// <-> right <-> top <-> left <-> bottom
// we can move the middle by moving each side. We can reverse a move by moving the other two in the same direction.
func swap(left []uint8, right []uint8) {
var temp [3]uint8
for i, l := range left {
temp[i] = l
}
for i, r := range right {
left[i] = r
}
for i, t := range temp {
right[i] = t
}
}
// Spin spins the cube once
// the spin is clockwise on positive axis 0, 1, 2 = z, y, x
// the spin is symetric about the origin (counter) for 3, 4, 5, = -z, -y, -x
// 3 moves on a single face reverses a move
// a move of the middle is equivelent to one move on one side and 3 on the other
func (c *Cube) Spin(side uint8) {
if side < 0 || side >= 6 {
panic("Cube.Spin(side): Invalid size!")
}
// 0 1 2 \< 2 3 4
// 7 3 |^ 1 5
// 6 5 4 /> 0 7 6
fa, ce := c[side][:2], c[side][2:NumTiles]
for i, elem := range append(ce, fa...) {
c[side][i] = elem
}
// turning faces affects related sides
var next [3]uint8
for i := range c {
if ((int(side) + NumSides - i) % (NumSides / 2)) == 0 {
continue
}
var temp [3]uint8
switch side {
case 0:
switch i {
case 1:
fallthrough
case 4:
// 6 5 4
swap(c[i][4:7], next[:])
case 2:
fallthrough
case 5:
// 6 7 0
swap(temp[:], append(c[i][6:], c[i][0]))
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
swap(next[:], temp[:])
}
case 1:
switch i {
case 0:
fallthrough
case 2:
fallthrough
case 3:
// 0 1 2
swap(c[i][:3], next[:])
case 5:
// 4 5 6
swap(c[i][4:7], next[:])
}
case 2:
switch i {
case 0:
fallthrough
case 1:
// 2 3 4
swap(c[i][2:5], next[:])
case 3:
fallthrough
case 4:
// 6 7 0
swap(temp[:], append(c[i][6:], c[i][0]))
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
swap(next[:], temp[:])
}
case 3:
switch i {
case 1:
fallthrough
case 4:
// 0 1 2
swap(c[i][:3], next[:])
case 2:
fallthrough
case 5:
// 2 3 4
swap(c[i][2:5], next[:])
}
case 4:
switch i {
case 0:
fallthrough
case 2:
fallthrough
case 3:
// 6 5 4
swap(c[i][4:7], next[:])
case 5:
// 0 1 2
swap(c[i][:3], next[:])
}
case 5:
switch i {
case 0:
fallthrough
case 1:
// 6 7 0
swap(temp[:], append(c[i][6:], c[i][0]))
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
swap(next[:], temp[:])
case 3:
fallthrough
case 4:
// 2 3 4
swap(c[i][2:5], next[:])
}
}
}
i := 0
for ((int(side) + NumSides - i) % (NumSides / 2)) == 0 {
i++
}
// one last time, slide next into place.
switch side {
case 0:
switch i {
case 1:
fallthrough
case 4:
// 6 5 4
swap(c[i][4:7], next[:])
case 2:
fallthrough
case 5:
// 6 7 0
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
}
case 1:
switch i {
case 0:
fallthrough
case 2:
fallthrough
case 3:
// 0 1 2
swap(c[i][:3], next[:])
case 5:
// 4 5 6
swap(c[i][4:7], next[:])
}
case 2:
switch i {
case 0:
fallthrough
case 1:
// 2 3 4
swap(c[i][2:5], next[:])
case 3:
fallthrough
case 4:
// 6 7 0
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
}
case 3:
switch i {
case 1:
fallthrough
case 4:
// 0 1 2
swap(c[i][:3], next[:])
case 2:
fallthrough
case 5:
// 2 3 4
swap(c[i][2:5], next[:])
}
case 4:
switch i {
case 0:
fallthrough
case 2:
fallthrough
case 3:
// 6 5 4
swap(c[i][4:7], next[:])
case 5:
// 0 1 2
swap(c[i][:3], next[:])
}
case 5:
switch i {
case 0:
fallthrough
case 1:
// 6 7 0
c[i][6] = next[0]
c[i][7] = next[1]
c[i][0] = next[2]
case 3:
fallthrough
case 4:
// 2 3 4
swap(c[i][2:5], next[:])
}
}
}
// Shuffle shuffles the Rubik's Cube
func (c *Cube) Shuffle() {
var rands = make([]uint8, 1000)
rand.Read(rands)
for _, r := range rands {
c.Spin(r % uint8(NumSides))
}
}
// Solved iff each side is only a single color
func (c *Cube) Solved() bool {
// either 2+ sides are wrong or none
for s := 1; s < NumSides; s++ {
for i := range c[s] {
if c[s][i] != uint8(s) {
return false
}
}
}
return true
}
// Cross iff the center and middle edges are equivilent
func (c *Cube) Cross(si uint8) bool {
for _, i := range []uint8{2, 3, 5, 7} {
if si != c[si][i] {
return false
}
}
return true
}
// Daisy iff middle edges are equivilent
func (c *Cube) Daisy(si uint8) bool {
for _, i := range []uint8{3, 5, 7} {
if c[si][2] != c[si][i] {
return false
}
}
return true
}
// RAlg manipulates corners
func (c *Cube) RAlg(s1, s2 uint8) {
// X'3 == X
c.Spin(s1)
c.Spin(s1)
c.Spin(s1)
c.Spin(s2)
c.Spin(s2)
c.Spin(s2)
c.Spin(s1)
c.Spin(s2)
}
// BFSolve returns the shortest solution to the rubik's cube
func BFSolve(c Cube) Solution {
// Uses Breadth-First Search
var fringe, known = make(map[Cube]Solution), make(map[Cube]bool)
// initialize fringe to hold source
fringe[c] = nil
known[c] = true
for {
var newFringe = make(map[Cube]Solution)
for cube, path := range fringe {
for i := uint8(0); i < NumSides; i++ {
var next = cube
next.Spin(i)
sofar := append(append(Solution(nil), path...), i)
if next.Solved() {
return reduce(sofar)
} else if !known[next] {
known[next] = true
newFringe[next] = sofar
}
}
delete(fringe, cube)
}
fringe = newFringe
}
}
// Reduce reduces the solution to higher-level moves
func reduce(s Solution) Solution {
return s
}
// TwoCycleSolve is probs okay
func TwoCycleSolve(c Cube) Solution {
return nil
}
// TODO
func (s *Solution) String() string {
return ""
}