Skip to content

Instantly share code, notes, and snippets.

@abhijitmamarde
Created October 5, 2022 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhijitmamarde/e31944b56820602f0aa6078360c3258a to your computer and use it in GitHub Desktop.
Save abhijitmamarde/e31944b56820602f0aa6078360c3258a to your computer and use it in GitHub Desktop.
Gets the scrambler steps for a Rubiks cube, for the given solution
"""
For the given Rubik's cube solution, generates the scramble
For getting solution:
https://ruwix.com/widget/3d/?label=Sample%20Solve&alg=F%20R%20F%20R%20U%20U%20L%20R%20L%20R%20U%27&flags=showalg&colors=WCA
for this specific solution
F R F R U U L R L R U'
the scramble step is:
U R' L' R' L' U' U' R' F' R' F'
IFRAME CODE for above ex solution/scramble:
<iframe width="250" height="380" style="width: 250px; height: 380px; overflow: hidden;" src="https://ruwix.com/widget/3d/?label=Sample%20Solve&alg=F%20R%20F%20R%20U%20U%20L%20R%20L%20R%20U'&flags=showalg&colors=WCA" scrolling="no"></iframe>
using roofpig code: https://github.com/larspetrus/Roofpig
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="roofpig_and_three.min.js"></script>
<div class="roofpig" data-config="alg=F R F R U U L R L R U'|flags=showalg|colors=WCA">Sample Solve</div>
Creating the cube dynamically:
CubeAnimation.create_in_dom('#show-alg', "alg=F R F R U U L R L R U'", "class='my-roofpig'");
"""
import re
class Solution:
def __init__(self, solution_steps):
# self.steps_v1 = [x.strip() for x in solution_steps.strip(' ') if x.strip()]
self.steps = [x for x in re.split("\s", solution_steps) if x]
# def not_reduced(self):
# return ("'" in self.steps) or ('2' in self.steps)
# def reduce_v1(self):
# def reduce_char(ch):
# if ch in self.steps:
# x = self.steps.index(ch)
# self.steps = self.steps[:x] + self.steps[x+1:]
# self.steps[x-1] += ch
# return True
# return False
# while self.not_reduced():
# if reduce_char("'"):
# continue
# if reduce_char("2"):
# continue
# return " ".join(self.steps)
def reduce(self):
return " ".join(self.steps)
def get_scramble_steps(self):
"""Gets the steps for scrambling of the solution! Should be reduced first!
needed for v1 approach, not needed now!
"""
scramble_steps = []
for step in self.steps:
if len(step) == 2:
if step[-1] == "'":
scramble_steps.append(step[0])
elif step[-1] == "2":
scramble_steps.append(step)
else:
raise Exception(f"INVALID l2 step: {step}")
elif len(step) == 1:
scramble_steps.append(step + "'")
else:
raise Exception(f"INVALID step: {step}")
return " ".join(scramble_steps[::-1])
def test_reducer():
tests_data = [
{
"input_solution": """
F R F R U U L
R L
R
U'
""",
"expected_reduced": "F R F R U U L R L R U'",
"expected_scramble_steps":
# "F' R' F' R' U' U' L' R' L' R' U" # step1
"U R' L' R' L' U' U' R' F' R' F'" # reversed for step1
},
{
"input_solution": """
F2 R F' R U U L
R L
R2
U'
""",
"expected_reduced": "F2 R F' R U U L R L R2 U'",
"expected_scramble_steps":
# "F2 R' F R' U' U' L' R' L' R2 U"
"U R2 L' R' L' U' U' R' F R' F2"
},
]
for td in tests_data:
x = Solution(td['input_solution'])
final_solution = x.reduce()
print(f"{final_solution=}")
assert final_solution == td['expected_reduced']
print(" ... PASS 1")
scramble_steps = x.get_scramble_steps()
print(f"{scramble_steps=}")
assert scramble_steps == td['expected_scramble_steps']
print(" ... PASS 2")
if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
test_reducer()
else:
"F R F R U U L R L R U'"
r = Solution(sys.argv[1])
solution = r.reduce()
steps = r.get_scramble_steps()
print(f"steps =\"{steps!s}\"")
print(f"solution=\"{solution!s}\"")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment