7
\$\begingroup\$

Your challenge is to write a program to solve a 3x3x3 Rubik's Cube. This challenge is based on this one from 2013, rewritten to adhere to current community standards, and reposted with the original author's permission and help on meta.

Input

The input should represent an unsolved Rubik's Cube. You may read this input via any standard means, and the input can be in any format you choose, except a sequence of moves to get the cube to an unsolved state (or anything similar); that would trivialize this challenge.

That means that the input can look like this:

    UUU
    UUU
    UUU
 LLLFFFRRRBBB
 LLLFFFRRRBBB
 LLLFFFRRRBBB
    DDD
    DDD
    DDD

U representing cubies on the top/upper face, L representing the left, etc.

It could also look like a Cubically cube-dump, an array of characters/integers, or the weird format in the original challenge; however you like. You must specify how input should be taken in your answer.

You can translate a cube-dump to an ULFRBD scheme here or the other way around here.

Output

You will output, via any allowed means, the moves that must be performed on the inputted Rubik's Cube to return it to the solved state. You may use any chosen notation or method to describe rotation; please specify what you use in your answer.

I recommend that you use Singmaster's notation as it is the simplest and clearest:

R - turn the right face of the cube 90 degrees clockwise
L - turn the left face of the cube 90 degrees clockwise
U - turn the top face of the cube 90 degrees clockwise
D - turn the bottom face of the cube 90 degrees clockwise
F - turn the front face of the cube 90 degrees clockwise
B - turn the back face of the cube 90 degrees clockwise

Append ' to any move to make it counterclockwise and 2 to any move to make it 180 degrees.

If you are unsure of the validity of an I/O method, feel free to comment or ping me in chat.

Examples

Input is in the format of a cube-dump and a ULFRBD layout; output is in Singmaster's notation.

Input -> D'U'R'L'R'L2R'F2U'D'U'D'LR'B'F'U'D'L2R'
Input -> RF2U2R2ULB2R2U2R'L'DB2U2D2B'R'F'B2DFU2RU2L'
Input -> L2FL'R'FB'U2D'F'R'LBF2R2L'F2D2BL2F2RU2D'LF'
Input -> B'U'FURD'B'F'RBF2D'F2R2L2FU'R'U'R2L2F'B2R'F
Input -> R2FUF2D'FR'B'D2L2F'URB2R'U'D'R2L'UD'R2B2UD2

Your program may assume that the given cube is possible to solve; i.e. you do not need to handle the case that the inputted cube is unsolvable.

Restrictions

Answers like this, while valid/interesting on other challenges, are not welcome here. You may not use an algorithm that iterates through every possible state of the cube and prints the moves as it goes, or anything similar.

To define these restrictions, your program must be able to solve each of the test cases above on TIO. So it must:

  • Exit in under 60 seconds.
  • Output less than 128KiB.

Validation

To validate that your program indeed solves the Rubik's Cube, you can obviously use a physical cube or an online cube emulator by mixing it how you like, feeding its state into your program, and then performing the output moves on the cube.

However, if you choose to format your input as the cube dump (or the ULFRBD scheme and translate it to a cube dump), you can validate your program via Cubically's online interpreter like so:

  1. Go to the online interpreter.
  2. Type rs into the Code section.
  3. Paste your unsolved cube-dump into the Input section.
  4. Run your program with the unsolved cube-dump. Copy the output into the Footer section.
  5. Click Run. If your program is valid, Solved! will appear in the Output section.

Winning

As this is , the shortest code in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ Sandboxed post. \$\endgroup\$ – MD XF Jan 15 '18 at 22:31
  • 1
    \$\begingroup\$ things I'm assuming: 1. orientation doesn't matter as long as it's solved? 2. the solution does not have to be the shortest possible? \$\endgroup\$ – HyperNeutrino Jan 15 '18 at 23:15
  • \$\begingroup\$ @HyperNeutrino Those assumptions are correct. \$\endgroup\$ – MD XF Jan 15 '18 at 23:24
  • \$\begingroup\$ @HyperNeutrino If the solution has to be shortest possible, the challenge becomes "implement optimal mode of Cube Explorer". \$\endgroup\$ – user202729 Jan 16 '18 at 1:26
  • 1
    \$\begingroup\$ @MDXF my bad I read it as three restrictions. You said the same thing as my suggestion. \$\endgroup\$ – Jonathan Allan Jan 17 '18 at 6:40
6
\$\begingroup\$

Cubically, 3 bytes

rp▦

Try it online! You can use this to print a random cube.

Explanation:

r    read cube from stdin
 p   set PRINTMOVES flag to display moves as they are performed on the cube
  ▦  insert moves to solve the cube at the current point in the program

Capitalize the P to see prettier output (e.g. RLD'U2 as opposed to R1L1D3U2).

\$\endgroup\$
  • 1
    \$\begingroup\$ Talk about the right tool for the job. \$\endgroup\$ – eaglgenes101 Jun 2 '18 at 3:56
  • 8
    \$\begingroup\$ Have you considered that this is answer is not downvoted due to the trivial content, but because it was posted by the person who asked the question and wrote the programming language four years after the original challenge was posted? \$\endgroup\$ – Engineer Toast Jun 4 '18 at 20:29
  • \$\begingroup\$ @EngineerToast Four years? How? \$\endgroup\$ – user202729 Jun 8 '18 at 9:53
  • \$\begingroup\$ @EngineerToast well, I only posted/accepted it since there were no other answers. \$\endgroup\$ – MD XF Jun 18 '18 at 21:11
  • \$\begingroup\$ I understand that and I hesitated to comment. Waiting 9 days is probably enough. However, it appeared at first glance that the question was asked just to show off what you'd made and - right or wrong thought that may be - I bet it contributed to a poor reception. \$\endgroup\$ – Engineer Toast Jun 19 '18 at 14:43
2
\$\begingroup\$

Python + kociemba, 27 bytes

A non-trivial language, trivially using the kociemba library

from kociemba import*
solve

Call solve() with a cube in the ULFRBD scheme.

Example, solve('DRLUUBFBRBLURRLRUBLRDDFDLFUFUFFDBRDUBRUFLLFDDBFLUBLRBD') returns u"D2 R' D' F2 B D R2 D2 R' F2 D' F2 U' B2 L2 U2 D R2 U"

(PS, this answer isn't any "better" than the Cubically answer)

\$\endgroup\$
  • \$\begingroup\$ I think this is a non-standard library, so you should mention the library name in the language name, for example "Python + kociemba". \$\endgroup\$ – user202729 Jun 8 '18 at 9:47
  • 1
    \$\begingroup\$ Coincidentally the method is exactly the same. / You need to mention the builtin name (meta consensus) \$\endgroup\$ – user202729 Jun 8 '18 at 9:51
  • 1
    \$\begingroup\$ @user202729 actually, not coincidentally. That link is how I found the library. \$\endgroup\$ – RootTwo Jun 8 '18 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.