Skip to content

Instantly share code, notes, and snippets.

@cs0x7f
Created May 1, 2012 07:45
Show Gist options
  • Save cs0x7f/2566010 to your computer and use it in GitHub Desktop.
Save cs0x7f/2566010 to your computer and use it in GitHub Desktop.
Skewb Random State Scrambler
center = [0]*360;
corner = [0]*8748;
centermv = [[0 for col in range(4)] for row in range(360)];
cornermv = [[0 for col in range(4)] for row in range(8748)];
def cycle3(arr, i1, i2, i3):
c = arr[i1];
arr[i1] = arr[i2];
arr[i2] = arr[i3];
arr[i3] = c;
def getctmv(p, m):
ps = [0, 0, 0, 0, 0, 1];
s = 0;
for i in range(3, -1, -1):
p, ps[i] = divmod(p, 6-i);
s = s + ps[i];
for j in range(i+1, 6):
if (ps[j] >= ps[i]):
ps[j] += 1;
if (s % 2 == 1):
temp = ps[5];
ps[5] = ps[4];
ps[4] = temp;
if (m==0):#L
cycle3(ps, 0, 5, 1);
elif (m==1):#R
cycle3(ps, 0, 2, 4);
elif (m==2):#D
cycle3(ps, 1, 3, 2);
elif (m==3):#B
cycle3(ps, 3, 5, 4);
idx = 0;
for i in range(0, 4):
idx *= (6-i);
for j in range(i+1, 6):
if (ps[i] > ps[j]):
idx += 1;
return idx;
def getcnmv(p, m):
ps = [0] * 8;#twists
for i in range(0, 7):
p, ps[i] = divmod(p, 3);
ps[7] = (30 - ps[4] - ps[5] - ps[6]) % 3;
# fc = p;#the position of one free corners is enough
fcmv = [[1, 2, 0, 3], [2, 1, 3, 0], [3, 0, 2, 1], [0, 3, 1, 2]];
p = fcmv[m][p];
ps[m] = (ps[m] + 1) % 3;
if (m==0):
ps[5], ps[6], ps[4] = ps[4]+2, ps[5]+2, ps[6]+2;#TODO
elif (m==1):
ps[6], ps[7], ps[4] = ps[4]+2, ps[6]+2, ps[7]+2;#TODO
elif (m==2):
ps[7], ps[4], ps[5] = ps[4]+2, ps[5]+2, ps[7]+2;#TODO
elif (m==3):
ps[7], ps[5], ps[6] = ps[5]+2, ps[6]+2, ps[7]+2;#TODO
for i in range(6, -1, -1):
p = p * 3 + (ps[i] % 3);
return p;
def calcperm():
for p in range(0, 360):
center[p]=-1;
for m in range(0, 4):
centermv[p][m] = getctmv(p,m);
for p in range(0, 8748):
corner[p]=-1;
for m in range(0, 4):
cornermv[p][m] = getcnmv(p,m);
# glb = [-1] * (3149280);
# glb[0] = 0;
# for l in range(0, 11):
# n=0;
# for p in range(0, 3149280):
# if (glb[p]==l):
# for m in range(0, 4):
# q=p;
# for c in range(0, 2):
# a, b = divmod(q, 360);
# q = cornermv[a][m] * 360 + centermv[b][m];
# if (glb[q]==-1):
# glb[q]=l+1;
# n+=1;
# print n;
center[0]=0;
for l in range(0, 5):
n=0;
for p in range(0, 360):
if (center[p]==l):
for m in range(0, 4):
q=p;
for c in range(0, 2):
q = centermv[q][m];
if (center[q]==-1):
center[q]=l+1;
n+=1;
# print n;
corner[0]=0;
for l in range(0, 7):
n=0;
for p in range(0, 8748):
if (corner[p]==l):
for m in range(0, 4):
q=p;
for c in range(0, 2):
q = cornermv[q][m];
if (corner[q]==-1):
corner[q]=l+1;
n+=1;
# print n;
def search(sol, ct, cn, l, lm):
# searches for solution, from position q|t, in l moves exactly. last move was lm, current depth=d
if (l==0):
if (ct==0 and cn==0):
return True;
else:
if (center[ct]>l or corner[cn]>l):
return False;
for m in range(0, 4):
if (m!=lm):
p=ct;
s=cn;
for a in range(0, 2):
p = centermv[p][m];
s = cornermv[s][m];
sol.append('LRDB'[m]+' \''[a]);
if (search(sol, p, s, l-1, m)):
return 1;
sol.pop();
return 0;
def gets():
sol = [];
import random;
cn=random.randint(0, 8747);#3^7*4
ct=random.randint(0, 359);#6!/2
for l in range(0, 100):
if(search(sol, ct, cn, l,-1)):
break;
return ' '.join(sol);
calcperm();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment