Mouvements du Rubik’s cube 2×2×2. rubik(“AB-H2”) tourne la face Avant dans le sens des aiguilles d’une montre puis la face du Bas dans le sens inverse et enfin la face du Haut deux fois.
def rubik(m): i=0 while i<len(m): if m[i]=="-": m=m[:i]+m[i-1]*2+m[i+1:] elif m[i]=="2": m=m[:i]+m[i-1]+m[i+1:] elif m[i] not in "APDGHB": m=m[:i]+m[i+1:] i+=1 C={"A":["A"]*4,"P":["P"]*4,"D":["D"]*4,"G":["G"]*4,"H":["H"]*4,"B":["B"]*4} # H0H1 # H3H2 # # G0G1 A0A1 D0D1 P0P1 # G3G2 A3A2 D3D2 P3P2 # # B0B1 # B3B2 for c in m: C[c]=[C[c][3]]+C[c][:3] if c=="D": C["A"][1],C["H"][1],C["P"][3],C["B"][1]=C["B"][1],C["A"][1],C["H"][1],C["P"][3] C["A"][2],C["H"][2],C["P"][0],C["B"][2]=C["B"][2],C["A"][2],C["H"][2],C["P"][0] elif c=="A": C["G"][1],C["H"][2],C["D"][3],C["B"][0]=C["B"][0],C["G"][1],C["H"][2],C["D"][3] C["G"][2],C["H"][3],C["D"][0],C["B"][1]=C["B"][1],C["G"][2],C["H"][3],C["D"][0] elif c=="H": C["G"][1],C["P"][1],C["D"][1],C["A"][1]=C["A"][1],C["G"][1],C["P"][1],C["D"][1] C["G"][0],C["P"][0],C["D"][0],C["A"][0]=C["A"][0],C["G"][0],C["P"][0],C["D"][0] elif c=="G": C["A"][0],C["B"][0],C["P"][2],C["H"][0]=C["H"][0],C["A"][0],C["B"][0],C["P"][2] C["A"][3],C["B"][3],C["P"][1],C["H"][3]=C["H"][3],C["A"][3],C["B"][3],C["P"][1] elif c=="P": C["G"][0],C["B"][3],C["D"][2],C["H"][1]=C["H"][1],C["G"][0],C["B"][3],C["D"][2] C["G"][3],C["B"][2],C["D"][1],C["H"][0]=C["H"][0],C["G"][3],C["B"][2],C["D"][1] elif c=="B": C["G"][3],C["A"][3],C["D"][3],C["P"][3]=C["P"][3],C["G"][3],C["A"][3],C["D"][3] C["G"][2],C["A"][2],C["D"][2],C["P"][2]=C["P"][2],C["G"][2],C["A"][2],C["D"][2] print(" %s%s"%(C["H"][0],C["H"][1])) print(" %s%s"%(C["H"][3],C["H"][2])) print() print(" %s%s %s%s %s%s %s%s"%(C["G"][0],C["G"][1],C["A"][0],C["A"][1],C["D"][0],C["D"][1],C["P"][0],C["P"][1])) print(" %s%s %s%s %s%s %s%s"%(C["G"][3],C["G"][2],C["A"][3],C["A"][2],C["D"][3],C["D"][2],C["P"][3],C["P"][2])) print() print(" %s%s"%(C["B"][0],C["B"][1])) print(" %s%s"%(C["B"][3],C["B"][2]))