-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcubie.py
430 lines (376 loc) · 15.2 KB
/
cubie.py
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
# ####### The cube on the cubie level is described by the permutation and orientations of corners and edges ############
from defs import cornerFacelet, edgeFacelet, cornerColor, edgeColor, N_SYM
from enums import Color, Corner as Co, Edge as Ed, Move
import face
from misc import c_nk, rotate_left, rotate_right
from random import randrange
# ################## The basic six cube moves described by permutations and changes in orientation #####################
# Up-move
cpU = [Co.UBR, Co.URF, Co.UFL, Co.ULB, Co.DFR, Co.DLF, Co.DBL, Co.DRB] # corner permutation
coU = [0, 0, 0, 0, 0, 0, 0, 0] # corner orientation
epU = [Ed.UB, Ed.UR, Ed.UF, Ed.UL, Ed.DR, Ed.DF, Ed.DL, Ed.DB, Ed.FR, Ed.FL, Ed.BL, Ed.BR] # edge permutation
eoU = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # edge orientation
# Right-move
cpR = [Co.DFR, Co.UFL, Co.ULB, Co.URF, Co.DRB, Co.DLF, Co.DBL, Co.UBR] # permutation of the corners
coR = [2, 0, 0, 1, 1, 0, 0, 2] # changes of the orientations of the corners
epR = [Ed.FR, Ed.UF, Ed.UL, Ed.UB, Ed.BR, Ed.DF, Ed.DL, Ed.DB, Ed.DR, Ed.FL, Ed.BL, Ed.UR] # permutation of the edges
eoR = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # changes of the permutations of the edges
# Front-move
cpF = [Co.UFL, Co.DLF, Co.ULB, Co.UBR, Co.URF, Co.DFR, Co.DBL, Co.DRB]
coF = [1, 2, 0, 0, 2, 1, 0, 0]
epF = [Ed.UR, Ed.FL, Ed.UL, Ed.UB, Ed.DR, Ed.FR, Ed.DL, Ed.DB, Ed.UF, Ed.DF, Ed.BL, Ed.BR]
eoF = [0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0]
# Down-move
cpD = [Co.URF, Co.UFL, Co.ULB, Co.UBR, Co.DLF, Co.DBL, Co.DRB, Co.DFR]
coD = [0, 0, 0, 0, 0, 0, 0, 0]
epD = [Ed.UR, Ed.UF, Ed.UL, Ed.UB, Ed.DF, Ed.DL, Ed.DB, Ed.DR, Ed.FR, Ed.FL, Ed.BL, Ed.BR]
eoD = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# Left-move
cpL = [Co.URF, Co.ULB, Co.DBL, Co.UBR, Co.DFR, Co.UFL, Co.DLF, Co.DRB]
coL = [0, 1, 2, 0, 0, 2, 1, 0]
epL = [Ed.UR, Ed.UF, Ed.BL, Ed.UB, Ed.DR, Ed.DF, Ed.FL, Ed.DB, Ed.FR, Ed.UL, Ed.DL, Ed.BR]
eoL = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# Back-move
cpB = [Co.URF, Co.UFL, Co.UBR, Co.DRB, Co.DFR, Co.DLF, Co.ULB, Co.DBL]
coB = [0, 0, 1, 2, 0, 0, 2, 1]
epB = [Ed.UR, Ed.UF, Ed.UL, Ed.BR, Ed.DR, Ed.DF, Ed.DL, Ed.BL, Ed.FR, Ed.FL, Ed.UB, Ed.DB]
eoB = [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1]
########################################################################################################################
CUBE_OK = True
class CubieCube:
"""Represent a cube on the cubie level with 8 corner cubies, 12 edge cubies and the cubie orientations.
Is also used to represent:
1. the 18 cube moves
2. the 48 symmetries of the cube.
"""
def __init__(self, cp=None, co=None, ep=None, eo=None):
"""
Initializes corners and edges.
:param cp: corner permutation
:param co: corner orientation
:param ep: edge permutation
:param eo: edge orientation
"""
if cp is None:
self.cp = [Co(i) for i in range(8)] # You may not put this as the default two lines above!
else:
self.cp = cp[:]
if co is None:
self.co = [0] * 8
else:
self.co = co[:]
if ep is None:
self.ep = [Ed(i) for i in range(12)]
else:
self.ep = ep[:]
if eo is None:
self.eo = [0] * 12
else:
self.eo = eo[:]
def __str__(self):
"""Print string for a cubie cube."""
s = ''
for i in Co:
s = s + '(' + str(self.cp[i]) + ',' + str(self.co[i]) + ')'
s += '\n'
for i in Ed:
s = s + '(' + str(self.ep[i]) + ',' + str(self.eo[i]) + ')'
return s
def __eq__(self, other):
"""Define equality of two cubie cubes."""
if self.cp == other.cp and self.co == other.co and self.ep == other.ep and self.eo == other.eo:
return True
else:
return False
def to_facelet_cube(self):
"""Return a facelet representation of the cube."""
fc = face.FaceCube()
for i in Co:
j = self.cp[i] # corner j is at corner position i
ori = self.co[i] # orientation of C j at position i
for k in range(3):
fc.f[cornerFacelet[i][(k + ori) % 3]] = cornerColor[j][k]
for i in Ed:
j = self.ep[i] # similar for Es
ori = self.eo[i]
for k in range(2):
fc.f[edgeFacelet[i][(k + ori) % 2]] = edgeColor[j][k]
return fc
def corner_multiply(self, b):
"""Multiply this cubie cube with another cubie cube b, restricted to the corners. Does not change b."""
c_perm = [0] * 8
c_ori = [0] * 8
ori = 0
for c in Co:
c_perm[c] = self.cp[b.cp[c]]
ori_a = self.co[b.cp[c]]
ori_b = b.co[c]
if ori_a < 3 and ori_b < 3: # two regular cubes
ori = ori_a + ori_b
if ori >= 3:
ori -= 3
elif ori_a < 3 <= ori_b: # cube b is in a mirrored state
ori = ori_a + ori_b
if ori >= 6:
ori -= 3 # the composition also is in a mirrored state
elif ori_a >= 3 > ori_b: # cube a is in a mirrored state
ori = ori_a - ori_b
if ori < 3:
ori += 3 # the composition is a mirrored cube
elif ori_a >= 3 and ori_b >= 3: # if both cubes are in mirrored states
ori = ori_a - ori_b
if ori < 0:
ori += 3 # the composition is a regular cube
c_ori[c] = ori
for c in Co:
self.cp[c] = c_perm[c]
self.co[c] = c_ori[c]
def edge_multiply(self, b: 'CubieCube'):
""" Multiply this cubie cube with another cubiecube b, restricted to the edges. Does not change b."""
e_perm = [0] * 12
e_ori = [0] * 12
for e in Ed:
e_perm[e] = self.ep[b.ep[e]]
e_ori[e] = (b.eo[e] + self.eo[b.ep[e]]) % 2
for e in Ed:
self.ep[e] = e_perm[e]
self.eo[e] = e_ori[e]
def multiply(self, b: 'CubieCube'):
self.corner_multiply(b)
self.edge_multiply(b)
def move(self, m: Move):
""" Apply move m to CubieCube """
self.multiply(moveCube[m])
def inv_cubie_cube(self, d):
"""Store the inverse of this cubie cube in d."""
for e in Ed:
d.ep[self.ep[e]] = e
for e in Ed:
d.eo[e] = self.eo[d.ep[e]]
for c in Co:
d.cp[self.cp[c]] = c
for c in Co:
ori = self.co[d.cp[c]]
if ori >= 3:
d.co[c] = ori
else:
d.co[c] = -ori
if d.co[c] < 0:
d.co[c] += 3
def corner_parity(self):
"""Give the parity of the corner permutation."""
s = 0
for i in range(Co.DRB, Co.URF, -1):
for j in range(i - 1, Co.URF - 1, -1):
if self.cp[j] > self.cp[i]:
s += 1
return s % 2
def edge_parity(self):
"""Give the parity of the edge permutation. A solvable cube has the same corner and edge parity."""
s = 0
for i in range(Ed.BR, Ed.UR, -1):
for j in range(i - 1, Ed.UR - 1, -1):
if self.ep[j] > self.ep[i]:
s += 1
return s % 2
def symmetries(self):
"""Generate a list of the symmetries and antisymmetries of the cubie cube."""
from symmetries import symCube, inv_idx # not nice here but else we have circular imports
s = []
d = CubieCube()
for j in range(N_SYM):
c = CubieCube(symCube[j].cp, symCube[j].co, symCube[j].ep, symCube[j].eo)
c.multiply(self)
c.multiply(symCube[inv_idx[j]])
if self == c:
s.append(j)
c.inv_cubie_cube(d)
if self == d: # then we have antisymmetry
s.append(j + N_SYM)
return s
# ############################ functions to get and set coordinates ################################################
def get_twist(self):
"""Get the twist of the 8 corners. 0 <= twist < 2187"""
ret = 0
for i in range(Co.URF, Co.DRB):
ret = 3 * ret + self.co[i]
return ret
def set_twist(self, twist):
twistparity = 0
for i in range(Co.DRB - 1, Co.URF - 1, -1):
self.co[i] = twist % 3
twistparity += self.co[i]
twist //= 3
self.co[Co.DRB] = ((3 - twistparity % 3) % 3)
def get_flip(self):
"""Get the flip of the 12 edges. 0 <= flip < 2048"""
ret = 0
for i in range(Ed.UR, Ed.BR):
ret = 2 * ret + self.eo[i]
return ret
def set_flip(self, flip):
flipparity = 0
for i in range(Ed.BR - 1, Ed.UR - 1, -1):
self.eo[i] = flip % 2
flipparity += self.eo[i]
flip //= 2
self.eo[Ed.BR] = ((2 - flipparity % 2) % 2)
def get_slice(self):
"""Get the location of the UD-slice edges FR,FL,BL and BR ignoring their permutation.
0<= slice < 495"""
a = x = 0
# Compute the index a < (12 choose 4)
for j in range(Ed.BR, Ed.UR - 1, -1):
if Ed.FR <= self.ep[j] <= Ed.BR:
a += c_nk(11 - j, x + 1)
x += 1
return a
def set_slice(self, idx):
slice_edge = list(range(Ed.FR, Ed.BR + 1))
other_edge = [Ed.UR, Ed.UF, Ed.UL, Ed.UB, Ed.DR, Ed.DF, Ed.DL, Ed.DB]
a = idx # Location
for e in Ed:
self.ep[e] = -1 # Invalidate all edge positions
x = 4 # set slice edges
for j in Ed:
if a - c_nk(11 - j, x) >= 0:
self.ep[j] = slice_edge[4 - x]
a -= c_nk(11 - j, x)
x -= 1
x = 0 # set the remaining edges UR..DB
for j in Ed:
if self.ep[j] == -1:
self.ep[j] = other_edge[x]
x += 1
def get_slice_sorted(self):
"""Get the permutation and location of the UD-slice edges FR,FL,BL and BR.
0 <= slice_sorted < 11880"""
a = x = 0
edge4 = [0] * 4
# First compute the index a < (12 choose 4) and the permutation array perm.
for j in range(Ed.BR, Ed.UR - 1, -1):
if Ed.FR <= self.ep[j] <= Ed.BR:
a += c_nk(11 - j, x + 1)
edge4[3 - x] = self.ep[j]
x += 1
# Then compute the index b < 4! for the permutation in edge4
b = 0
for j in range(3, 0, -1):
k = 0
while edge4[j] != j + 8:
rotate_left(edge4, 0, j)
k += 1
b = (j + 1) * b + k
return 24 * a + b
def set_slice_sorted(self, idx):
slice_edge = [Ed.FR, Ed.FL, Ed.BL, Ed.BR]
other_edge = [Ed.UR, Ed.UF, Ed.UL, Ed.UB, Ed.DR, Ed.DF, Ed.DL, Ed.DB]
b = idx % 24 # Permutation
a = idx // 24 # Location
for e in Ed:
self.ep[e] = -1 # Invalidate all edge positions
j = 1 # generate permutation from index b
while j < 4:
k = b % (j + 1)
b //= j + 1
while k > 0:
rotate_right(slice_edge, 0, j)
k -= 1
j += 1
x = 4 # set slice edges
for j in Ed:
if a - c_nk(11 - j, x) >= 0:
self.ep[j] = slice_edge[4 - x]
a -= c_nk(11 - j, x)
x -= 1
x = 0 # set the remaining edges UR..DB
for j in Ed:
if self.ep[j] == -1:
self.ep[j] = other_edge[x]
x += 1
def get_corners(self):
"""Get the permutation of the 8 corners.
0 <= corners < 40320"""
perm = list(self.cp) # duplicate cp
b = 0
for j in range(Co.DRB, Co.URF, -1):
k = 0
while perm[j] != j:
rotate_left(perm, 0, j)
k += 1
b = (j + 1) * b + k
return b
def set_corners(self, idx):
self.cp = [i for i in Co]
for j in Co:
k = idx % (j + 1)
idx //= j + 1
while k > 0:
rotate_right(self.cp, 0, j)
k -= 1
# ##################################################################################################################
# ############################################ other useful functions #############################################
def randomize(self):
"""Generate a random cube. The probability is the same for all possible states."""
def set_edges(idx):
"""The permutation of the 12 edges. 0 <= idx < 12!."""
self.ep = [i for i in Ed]
for j in Ed:
k = idx % (j + 1)
idx //= j + 1
while k > 0:
rotate_right(self.ep, 0, j)
k -= 1
set_edges(randrange(479001600)) # 12!
p = self.edge_parity()
while True:
self.set_corners(randrange(40320)) # 8!
if p == self.corner_parity(): # parities of edge and corner permutations must be the same
break
self.set_flip(randrange(2048)) # 2^11
self.set_twist(randrange(2187)) # 3^7
def verify(self):
"""Check if cubiecube is valid."""
edge_count = [0] * 12
for i in Ed:
edge_count[self.ep[i]] += 1
for i in Ed:
if edge_count[i] != 1:
return 'Error: Some edges are undefined.'
s = 0
for i in Ed:
s += self.eo[i]
if s % 2 != 0:
return 'Error: Total edge flip is wrong.'
corner_count = [0] * 8
for i in Co:
corner_count[self.cp[i]] += 1
for i in Co:
if corner_count[i] != 1:
return 'Error: Some corners are undefined.'
s = 0
for i in Co:
s += self.co[i]
if s % 3 != 0:
return 'Error: Total corner twist is wrong.'
if self.edge_parity() != self.corner_parity():
return 'Error: Wrong edge and corner parity'
return CUBE_OK
########################################################################################################################
# ################################## these cubes represent the basic cube moves ########################################
basicMoveCube = [CubieCube()] * 6
basicMoveCube[Color.U] = CubieCube(cpU, coU, epU, eoU)
basicMoveCube[Color.R] = CubieCube(cpR, coR, epR, eoR)
basicMoveCube[Color.F] = CubieCube(cpF, coF, epF, eoF)
basicMoveCube[Color.D] = CubieCube(cpD, coD, epD, eoD)
basicMoveCube[Color.L] = CubieCube(cpL, coL, epL, eoL)
basicMoveCube[Color.B] = CubieCube(cpB, coB, epB, eoB)
########################################################################################################################
# ################################# these cubes represent all 18 cube moves ############################################
moveCube = [CubieCube()] * 18
for c1 in Color:
cc = CubieCube()
for k1 in range(3):
cc.multiply(basicMoveCube[c1])
moveCube[3 * c1 + k1] = CubieCube(cc.cp, cc.co, cc.ep, cc.eo)
########################################################################################################################