-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_5_15.sage
329 lines (270 loc) · 7.75 KB
/
problem_5_15.sage
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
'''by lebedkin Alex
system of coordinate:
0X - left to right >
0Y - bottom to top ^
0Z - front to rear .
'''
import pprint
from collections import deque
#size of Rubik's cube NxNxN
N = 2
#3d array correspond to faces of Rubic's cube in the assembled state
cube3D = [[[0 for x in range(N+2)] for y in range(N+2)] for z in range(N+2)]
#each face contain NxN little colored squars
#tatal count of such squars are:
countOfSqrs = 0
#list of easy turns of cube
generators = list()
xTurns = list()
yTurns = list()
zTurns = list()
def faceDoesntContainFixedSqrs(cube3D,x1,y1,z1, x2,y2,z2):
for x in xrange(x1,x2+1):
for y in xrange(y1,y2+1):
for z in xrange(z1,z2+1):
if cube3D[x][y][z] == -1:
return False
return True
def faceZIsAbleToRorate(cube3D,Z):
return faceDoesntContainFixedSqrs(cube3D,0,0,Z, N+1,N+1,Z)
def generateAndReturnCubesItem( cube3D, x, y, z ):
global countOfSqrs
if cube3D[x][y][z] == 0:
countOfSqrs += 1
cube3D[x][y][z] = countOfSqrs
return cube3D[x][y][z]
def numberedAllFaces(cube3D):
#top face
y = N+1
for z in range(N,0,-1):
for x in range(1,N+1):
generateAndReturnCubesItem(cube3D,x,y,z)
#left face
x = 0
for y in range(N,0,-1):
for z in range(N,0,-1):
generateAndReturnCubesItem(cube3D,x,y,z)
#front face
z = 0
for y in range(N,0,-1):
for x in range(1,N+1):
generateAndReturnCubesItem(cube3D,x,y,z)
#right face
x = N+1
for y in range(N,0,-1):
for z in range(1,N+1):
generateAndReturnCubesItem(cube3D,x,y,z)
#rear face
z = N+1
for y in range(N,0,-1):
for x in range(N,0,-1):
generateAndReturnCubesItem(cube3D,x,y,z)
#bottom face
y = 0
for z in range(1,N+1):
for x in range(1,N+1):
generateAndReturnCubesItem(cube3D,x,y,z)
def printNetOfCube(A):
#top face
print " "+(' ' * N)+" +"+('---' * N)+"-+"+(' ' * N)+" "+(' ' * N)+" "
y = N+1
for z in range(N,0,-1):
print " "+(' ' * N)+" |",
for x in range(1,N+1):
print "%2d"%(A[x][y][z]),
print "|"
print "+"+('---' * N)+"-+"+('---' * N)+"-+"+('---' * N)+"-+"+('---' * N)+"-+"
for y in xrange(N,0,-1):
#left face
print "|",
for i in xrange(N):
print "%2d"%(A[1-1][y][N-i]),
#front face
print "|",
for i in xrange(N):
print "%2d"%(A[1+i][y][1-1]),
#right face
print "|",
for i in xrange(N):
print "%2d"%(A[N+1][y][1+i]),
#rear face
print "|",
for i in xrange(N):
print "%2d"%(A[N-i][y][N+1]),
print "|"
print "+"+('---' * N)+"-+"+('---' * N)+"-+"+('---' * N)+"-+"+('---' * N)+"-+"
#bottom face
y = 0
for z in range(1,N+1):
print " "+(' ' * N)+" |",
for x in range(1,N+1):
print "%2d"%(A[x][y][z]),
print "|"
print " "+(' ' * N)+" +"+('---' * N)+"-+"+(' ' * N)+" "+(' ' * N)+" "
def printNetOfCubeCorrespondingPermutation(permutationGroupElement):
#sage.groups.perm_gps.permgroup_element.PermutationGroupElement
p = Permutation(permutationGroupElement)
global cube3D
tmp = [[[0 for x in range(N+2)] for y in range(N+2)] for z in range(N+2)]
#renumbered all faces of cube
for x in xrange(N+2):
for y in xrange(N+2):
for z in xrange(N+2):
if cube3D[x][y][z] == -1:
tmp[x][y][z] = -1
elif cube3D[x][y][z] == 0:
tmp[x][y][z] = 0
else:
tmp[x][y][z] = p[cube3D[x][y][z]-1]
printNetOfCube(tmp)
# generator of contrclockwise rotate in the plane Z == const
def makeGeneratorOfZFace(A,Z):
result = ""
#rotate in Z == const plane
for i in xrange(1,N+1):
result += "(%d,%d,%d,%d)" % ( A[i][N+1][Z], A[N+1][N-i+1][Z], A[N-i+1][1-1][Z], A[1-1][i][Z] )
#if current face insn't inner face
if Z == 1 or Z == N:
if Z == 1:
Z = 0
if Z == N:
Z = N+1
'''
M - size of square
for example:
face =
+--------------+
| 1 2 3 |
| 4 9 5 |
| 6 7 8 |
+--------------+
M = 3, square =
+--------------+
| 1 2 3 |
| 4 . 5 |
| 6 7 8 |
+--------------+
M = 1, square =
+--------------+
| . . . |
| . 9 . |
| . . . |
+--------------+
'''
for M in range(N,0,-2):
dXY = (N - M) / 2
for i in range(1,M):
result += "(%d,%d,%d,%d)" % (A[i+dXY][N-dXY][Z], A[N-dXY][N-i+1-dXY][Z], A[N-i+1-dXY][1+dXY][Z], A[1+dXY][i+dXY][Z] )
return result
def turnCubeLeft(cube3D):
tmp = [[[0 for x in range(N+2)] for y in range(N+2)] for z in range(N+2)]
for x in xrange(0,N+2):
for y in xrange(0,N+2):
for z in xrange(0,N+2):
tmp[x][y][z] = cube3D[N+2 - z - 1][y][x]
return tmp
def turnCubeForward(cube3D):
tmp = [[[0 for x in range(N+2)] for y in range(N+2)] for z in range(N+2)]
for x in xrange(0,N+2):
for y in xrange(0,N+2):
for z in xrange(0,N+2):
tmp[x][y][z] = cube3D[x][z][N+2 - y - 1]
return tmp
def farthestVertex(start):
resDistance = 0
resVertex = start
counter = 0
fivePercentCounterBarrier = 3674160 // 100
d = deque()
d.append( start )
distance = {start:0}
while (d):
current = d.popleft()
currentDistance = distance[current]
if currentDistance > resDistance:
resDistance = currentDistance
resVertex = current
for turn in xTurns+yTurns+zTurns:
#90 degree turn
tmp = current * turn
if( not tmp in distance ):
distance[tmp] = currentDistance + 1
d.append( tmp )
counter += 1
#180 degree turn
tmp = tmp*turn
if( not tmp in distance ):
distance[tmp] = currentDistance + 1
d.append( tmp )
counter += 1
#270 degree turn
tmp = tmp*turn
if( not tmp in distance ):
distance[tmp] = currentDistance + 1
d.append( tmp )
counter +=1
#show current progress
if counter % fivePercentCounterBarrier < 3:
sys.stdout.write("\rBFS %d%% completed" % (counter // fivePercentCounterBarrier))
sys.stdout.flush()
counter += 3
print "."
return resVertex, resDistance
def diameterOfGraphOfGroup():
Sn = SymmetricGroup(countOfSqrs)
#the identity permutation correspond the initial state of the Rubic's cube
E = Sn.identity()
V,d = farthestVertex(E)
return farthestVertex(V)[1] #return only distance
def godsNumberOfCube():
Sn = SymmetricGroup(countOfSqrs)
return farthestVertex(Sn.identity())[1] #return only distance
def main():
for x in xrange(0,N+2):
for y in xrange(0,N+2):
for z in xrange(0,N+2):
cube3D[x][y][z] = 0
if N % 2 == 0:
#fix the corner of cube
cube3D[0][1][1] = cube3D[1][0][1] = cube3D[1][1][0] = -1
else:
#fix centers of the faces
cube3D[0][(N+1)//2][(N+1)//2] = -1
cube3D[N+1][(N+1)//2][(N+1)//2] = -1
cube3D[(N+1)//2][0][(N+1)//2] = -1
cube3D[(N+1)//2][N+1][(N+1)//2] = -1
cube3D[(N+1)//2][(N+1)//2][0] = -1
cube3D[(N+1)//2][(N+1)//2][N+1] = -1
numberedAllFaces(cube3D)
printNetOfCube(cube3D)
Sn = SymmetricGroup(countOfSqrs)
tempCube = cube3D
for z in range(1,N+1):
if faceZIsAbleToRorate(tempCube,z):
generators.append( makeGeneratorOfZFace(tempCube,z) )
zTurns.append( Sn(generators[-1]) )
tempCube = turnCubeLeft(tempCube)
for z in range(1,N+1):
if faceZIsAbleToRorate(tempCube,z):
generators.append( makeGeneratorOfZFace(tempCube,z) )
xTurns.append( Sn(generators[-1]) )
tempCube = turnCubeForward(tempCube)
for z in range(1,N+1):
if faceZIsAbleToRorate(tempCube,z):
generators.append( makeGeneratorOfZFace(tempCube,z) )
yTurns.append( Sn(generators[-1]) )
pp = pprint.PrettyPrinter(width = 70, depth=6, indent=4)
print "Generators of Group ="
pp.pprint(generators)
cubeGroup = PermutationGroup(generators)
print "Cube.order() = ", factor( cubeGroup.order() ), " = ", cubeGroup.order()
randomCube = cubeGroup.random_element()
print "random item = ", randomCube
printNetOfCubeCorrespondingPermutation(randomCube)
orbits = cubeGroup.orbits()
print "orbits = ", orbits
if( N == 2 ):
godsNumber = godsNumberOfCube()
print "God's number = ", godsNumber
if __name__ == '__main__':
main()