-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_order.py
181 lines (140 loc) · 4.19 KB
/
sequence_order.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
from rubikscubennnsolver.RubiksCube444 import RubiksCube444, solved_4x4x4
from pprint import pprint
import os, re
import time
from sympy.core.numbers import ilcm
"""
Sequence Order
This file defines a method to compute
the order of a given sequence.
Charles Reid
January 2018
"""
def get_cube():
"""
Get a 4x4 Rubiks Cube.
"""
order = 'URFDLB'
cube = RubiksCube444(solved_4x4x4, order)
return cube
def get_cube():
"""
Get a 4x4 Rubiks Cube.
"""
order = 'URFDLB'
cube = RubiksCube444(solved_4x4x4, order)
return cube
def main():
# Built in tuple representation has the problem
# that it treats all colors as interchangeable
# We need a tuple that treats each of the faces
# or pieces as separate, independent, but connected
# units.
#sequences = ['U U','D D','R R','L L','F F','B B']
#sequences = ['U U U','D D D','R R R','L L L','F F F','B B B']
#sequences = ["R","U R U' R'","U R"]
#sequences = ['U','D','L','R','F','B']
#sequences = ["U R U' R'"]
sequences = ["Uw Rw","U Rw"]
center_squares = [ 6, 7, 10, 11,
22, 23, 26, 27,
38, 39, 42, 43,
54, 55, 58, 59,
70, 71, 74, 75,
86, 87, 90, 91]
for seq in sequences:
print("-"*40)
print(seq)
# do it.
factors_list = factor_rotation(seq)
factors_len = set()
for factor in factors_list:
if(True or set(factor) not in center_squares):
factors_len.add(len(factor))
print("Factor sizes: %s"%(factors_len))
print("Factors:")
print_factors(factors_list)
print("Least common multiple: %d"%( ilcm(*factors_len) ))
def factor_rotation(rot):
"""
For a given rotation, factor the resulting permutation.
"""
cube0 = list(range(1,96+1))
cube1 = cube0.copy()
cube_prior = cube0.copy()
r = get_cube()
sequence = []
# Needed to fix this to use the prior cube,
# otherwise multiple move sequences were broken.
for c,move in enumerate(rot.split(" ")):
rotmap = r.rotation_map(move)
for m in rotmap:
# shift item at index m[0] to item at index m[1]
cube1[cube_prior.index(m[0])] = m[1]
cube_prior = cube1.copy()
print("\n")
print_cube(cube0)
print("\n")
print_cube(cube1)
print("\n")
factors = factor_permutation(cube0,cube1)
return factors
def factor_permutation(perm_top,perm_bot):
"""
Factor a permutation into its lowest terms
"""
MAX = 96
# Need a way to also mark them as used... bit vector
used_vector = [0,]*len(perm_top)
i = 0
start = perm_top[0]
used_vector[0] = 1
factors = []
# If we still have values to pick out:
while(0 in used_vector):
factor = []
while(True):
used_vector[i] = 1
leader = perm_top[i]
follower = perm_bot[i]
i = perm_top.index(follower)
while(used_vector[i]==1):
i += 1
if(i>=MAX):
break
if(i>=MAX):
break
elif(follower==start):
break
else:
factor.append(follower)
# add start to end
factor.append(start)
factors.append(factor)
try:
#import pdb; pdb.set_trace()
i = used_vector.index(0)
start = perm_top[i]
except ValueError:
break
factorsize = set()
check = 0
for factor in factors:
factorsize.add(len(factor))
check += len(factor)
return factors
def print_cube(cube):
#print("(" + " ".join("%02d"%(j) for j in cube) + ")")
print("(" + " ".join("%02d"%(j) for j in cube) + ")")
def print_factors(factors_list):
ones_list = []
for factor in factors_list:
if(len(factor)>1):
# Length > 1: print the factor separately
print(factor)
else:
# Length==1: collect and print together
ones_list.append(factor[0])
print("Independent Faces: %s"%(ones_list))
if __name__=="__main__":
main()