Created
October 31, 2016 15:06
-
-
Save AlphaSheep/f48d494c83db878606a24cc38cbc643b to your computer and use it in GitHub Desktop.
Scripts for generating TSLE algorithms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' | |
Created on 19 May 2016 | |
Copyright (c) 2016 Brendan Gray and Sylvermyst Technologies | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
''' | |
import copy | |
solvedCube = [[0,0,0,0,0,0,0,0], [0,1,2,3,4,5,6,7], [0,1,2,3,4,5,6,7,8,9,10,11]] | |
m = { | |
"U" : [[0,0,0,0, 0,0,0,0], [1,2,3,0, 4,5,6,7], [1,2,3,0, 4,5,6,7, 8,9,10,11]], # | |
"U2": [[0,0,0,0, 0,0,0,0], [2,3,0,1, 4,5,6,7], [2,3,0,1, 4,5,6,7, 8,9,10,11]], # | |
"U'": [[0,0,0,0, 0,0,0,0], [3,0,1,2, 4,5,6,7], [3,0,1,2, 4,5,6,7, 8,9,10,11]], # | |
"R" : [[0,1,2,0, 0,2,1,0], [0,5,1,3, 4,6,2,7], [0,5,2,3, 4,9,1,7, 8,6,10,11]], # | |
"R2": [[0,0,0,0, 0,0,0,0], [0,6,5,3, 4,2,1,7], [0,9,2,3, 4,6,5,7, 8,1,10,11]], # | |
"R'": [[0,1,2,0, 0,2,1,0], [0,2,6,3, 4,1,5,7], [0,6,2,3, 4,1,9,7, 8,5,10,11]] # | |
} # | |
triggers = ["", "R U R' ", "R U2 R' ", "R U' R' "] | |
aufs = ["", "U ", "U2 ", "U' "] | |
def memoize(f): | |
f.cache = {} | |
def memoizer(*args, **kwargs): | |
if args not in f.cache: | |
f.cache[args] = f(*args, **kwargs) | |
return f.cache[args] | |
return memoizer | |
def strCase(case): | |
s = '' | |
for i in range(len(case[2])): | |
if case[2][i]==6: | |
s += '['+str(i)+']' | |
for i in range(4): | |
s += ' '+str(case[0][i]) | |
return s | |
def algStrToList(algstr): | |
alg = [] | |
strippedAlg = algstr.strip().split(' ') | |
for move in strippedAlg: | |
if not move: | |
continue | |
alg.append(m[move.strip()]) | |
return alg | |
algStrToList = memoize(algStrToList) | |
def applyAlg(alg, startCase=solvedCube): | |
if type(alg) == str: | |
alg = algStrToList(alg) | |
result = copy.deepcopy(startCase) # don't accidentally change the startCase | |
for move in alg: | |
step = copy.deepcopy(result) | |
# corner permutation: | |
for i in range(len(move[1])): | |
step[0][move[1][i]] = result[0][i] | |
step[1][move[1][i]] = result[1][i] | |
# edge permutation: | |
for i in range(len(move[2])): | |
step[2][move[2][i]] = result[2][i] | |
# Note. We're done with the old result. We need to update orientations based on step now. | |
# corner orientation: | |
for i in range(len(move[0])): | |
step[0][i] = (step[0][i] + move[0][i]) % 3 | |
result = copy.deepcopy(step) | |
return result | |
def isSolvedTLSE(case): | |
for ep in range(4,12): | |
if not case[2][ep] == ep: | |
return False | |
for cp in [4,5,7]: | |
if not case[1][cp] == cp: | |
return False | |
for co in range(8): | |
if not case[0][co] == 0: | |
return False | |
return True | |
def preAUF(case): | |
for i in range(3): | |
case[i] = case[i][1:4]+[case[i][0]] + case[i][4:] | |
return case | |
def postAUF(case): | |
for i in range(len(case[1])): | |
if case[1][i] in range(4): | |
case[1][i] = (case[1][i]+1)%4 | |
for i in range(len(case[2])): | |
if case[2][i] in range(4): | |
case[2][i] = (case[2][i]+1)%4 | |
return case | |
def getUniqueCases(cases): | |
uniqueCases = {}; | |
for case in cases: | |
isUnique = True | |
for _ in range(4): | |
case = preAUF(case) | |
for _ in range(4): | |
case = postAUF(case) | |
s = strCase(case) | |
if s in uniqueCases.keys(): | |
isUnique = False | |
uniqueCases[s][1] += 1 | |
break | |
if not isUnique: | |
break | |
if isUnique: | |
uniqueCases[s] = [case, 1] | |
return uniqueCases | |
def getTSLECases(): | |
coCases = [] | |
# Twist the U layer corners | |
for c0 in range(3): | |
for c1 in range(3): | |
for c2 in range(3): | |
for c3 in range(3): | |
c6 = (9-(c0+c1+c2+c3)) % 3 | |
co = [c0,c1,c2,c3,0,0,c6,0] | |
coCases.append(co) | |
# Move the edge | |
epCases = [[0,1,2,3,4,5,6,7,8,9,10,11], | |
[6,0,2,3,4,5,1,7,8,9,10,11]] | |
cases = [] | |
for epCase in epCases: | |
for coCase in coCases: | |
cases.append([coCase, [1,2,3,0, 4,5,6,7], epCase]) | |
return cases | |
def getAllTSLEAlgs(triggers, aufs): | |
algs = [] | |
for auf1 in aufs: | |
for trig1 in triggers: | |
for auf2 in aufs: | |
for trig2 in triggers: | |
for auf3 in aufs: | |
for trig3 in triggers: | |
alg = (auf1+trig1+auf2+trig2+auf3+trig3).strip() | |
if not alg in algs: | |
algs.append(alg) | |
return algs#[:100] | |
def assignAlgsToCases(algs, cases): | |
caseNames = sorted(list(cases.keys())) | |
for s in caseNames: | |
case = cases[s][0] | |
print(s, end="\t") | |
for alg in algs: | |
newCase = applyAlg(alg, case) | |
if isSolvedTLSE(newCase): | |
cases[s].append(alg) | |
print('Solved by', len(cases[s])-2, end='') | |
if len(cases[s])-2: | |
print(' \t[',cases[s][2],']') | |
else: | |
print() | |
return cases | |
print('Identifying TSLE Cases:\n') | |
cases = getUniqueCases(getTSLECases()) | |
caseNames = list(cases.keys()) | |
caseNames.sort() | |
numAllCases = 0 | |
for i in range(len(caseNames)): | |
s = caseNames[i] | |
print(i,'\tCase:', s, 'Count:', cases[s][1]) | |
numAllCases += cases[s][1] | |
print() | |
print('# Unique Cases:', len(caseNames)-1) | |
print('# Non-unique Cases:', numAllCases-1) | |
print("-------------------------------------------") | |
#print(cases['[6] 0 0 0 0'][0]) | |
#print(isSolvedTLSE(cases['[6] 0 0 0 0'][0])) | |
print('Number of possible algorithms') | |
algs = getAllTSLEAlgs(triggers, aufs) | |
print(len(algs)) | |
print() | |
cases = assignAlgsToCases(algs, cases) | |
print("Done") | |
outFile = open('TSLE.txt', 'w') | |
caseNames = sorted(list(cases.keys())) | |
for s in caseNames: | |
line = s + '\t' + "({:.2f}".format(cases[s][1]/numAllCases*100) + ' %)' | |
for i in range(len(cases[s])-2): | |
line += '\t[' + cases[s][i+2] + ']' | |
outFile.write(line+'\n') | |
outFile.close() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
''' | |
Created on 19 May 2016 | |
Copyright (c) 2016 Brendan Gray and Sylvermyst Technologies | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
''' | |
infile = open('TSLE.txt') | |
outfile = open('TSLEv2OUT.txt', 'w') | |
triggerReplacements = [["R U R'", "A"], | |
["R U' R'", "B"], | |
["R U2 R'", "C"], | |
["U", "."], | |
["A . C", "s"], | |
["C .' B", "z"]] | |
allTriggers = 'ABCsz' | |
cases = []; | |
for line in infile: | |
lineParts = line.strip().split('\t') | |
algs = lineParts[2:] | |
lineParts[0] = lineParts[0].replace('[0]','[UB]').replace('[6]','[FR]').split(' ') | |
twistSum = 0 | |
nTwists = 0 | |
for i in range(1,len(lineParts[0])): | |
twistSum += int(lineParts[0][i]) | |
lineParts[0].append(str((12-twistSum)%3)) | |
for i in range(1,len(lineParts[0])): | |
if lineParts[0][i] == '0': | |
lineParts[0][i] = '' | |
elif i<5: | |
nTwists += 1 | |
cases.append(lineParts[0]) | |
counts = [] | |
pureAlgs = [] | |
for i in range(len(algs)): | |
pureAlgs.append(algs[i].strip('[]')) | |
for trigger in triggerReplacements: | |
algs[i] = algs[i].replace(*trigger) | |
algs[i] = algs[i].strip('[]').rstrip(" .2'") | |
# Count triggers | |
thisCount = 0 | |
for m in algs[i]: | |
if m in 'ABC': | |
thisCount += 1 | |
if m in 'sz': | |
thisCount += 2 | |
counts.append(thisCount) | |
minCount = min([4]+counts) | |
for i in range(len(algs)-1,-1,-1): | |
if (counts[i] > minCount) or \ | |
(".." in algs[i].replace(" ","").replace("2","").replace("'","")) or \ | |
(algs[i] in algs[:i]): | |
algs.pop(i) | |
algs.sort(key = lambda x: (x.replace(" ","").replace("2","").replace("'","").replace(".",""))) | |
fingerprints = ['' for _ in range(6)] | |
startTrigger = '' | |
startTriggers = ['' for _ in allTriggers] | |
triggers = ['' for _ in allTriggers] | |
for i in range(len(algs)): | |
base = (algs[i].replace(" ","").replace("2","").replace("'","").replace(".","")) | |
fingerprints[i] = base | |
if base[:1] not in startTrigger: | |
startTrigger += base[:1] | |
for t in range(len(allTriggers)): | |
if allTriggers[t] in base and allTriggers[t] not in triggers: | |
triggers[t] = allTriggers[t] | |
if allTriggers[t] in base[:1] and allTriggers[t] not in startTriggers: | |
startTriggers[t] = allTriggers[t] | |
canReduce = '' | |
for i in range(len(algs)): | |
if (len(fingerprints[i])>1) and fingerprints[i][1] in 'sz': | |
if 'X->Sune' not in canReduce: | |
canReduce += 'X->Sune' | |
if (len(fingerprints[i])>1) and fingerprints[i][0] in 'sz': | |
if 'Sune->X' not in canReduce: | |
canReduce += 'Sune->X' | |
triggers = '\t'.join(triggers) | |
startTriggers = '\t'.join(startTriggers) | |
imageURL = '' | |
firstAlg = '' | |
if len(pureAlgs)>0: | |
imageURL = 'http://stachu.cubing.net/v/visualcube.php?fmt=png&r=y25x-30z-2&sch=yrbyog&fd=uuuuuuuuunnnrrrnrrnnnfffffnuuuuuuuuunnnllllllnnnbbbbbb&case=' | |
imageURL += pureAlgs[0].replace("'","%27").replace(" ","") | |
firstAlg = pureAlgs[0] | |
while len(algs)<6: | |
algs.append('') | |
outfile.write('=IMAGE("'+imageURL+'")' + '\t' + firstAlg + '\t' + | |
'\t'.join(lineParts[0])+'\t'+str(minCount) + '\t' + str(nTwists) + | |
'\t'+startTriggers+'\t'+triggers+'\t'+ canReduce + '\t' + | |
'\t'.join(fingerprints)+'\t' + '\t'.join(algs) + '\t' + | |
imageURL +'\t' + '=IMAGE("'+imageURL+'")' + '\n') | |
outfile.close() | |
infile.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment