Solve a Rubik's Cube

7

1

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!

MD XF

Posted 2018-01-15T22:30:15.897

Reputation: 11 605

Sandboxed post. – MD XF – 2018-01-15T22:31:31.530

1things 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? – HyperNeutrino – 2018-01-15T23:15:48.097

@HyperNeutrino Those assumptions are correct. – MD XF – 2018-01-15T23:24:14.667

@HyperNeutrino If the solution has to be shortest possible, the challenge becomes "implement optimal mode of Cube Explorer". – user202729 – 2018-01-16T01:26:52.010

Under the hood many if not all will perform some move iteration for searching which makes much of the first restriction a do x without y; the time limit and output size restrictions should be enough on their own. – Jonathan Allan – 2018-01-16T07:26:10.840

@JonathanAllan Without the first restriction, answers could take millions of years (as the linked one does), which leads to uninteresting and untestable answers. – MD XF – 2018-01-17T00:20:29.757

1@MDXF my bad I read it as three restrictions. You said the same thing as my suggestion. – Jonathan Allan – 2018-01-17T06:40:50.993

Anyone feel like golfing the DeepCube solution?

– JayCe – 2018-06-18T03:15:50.360

@JayCe is there source code? – MD XF – 2018-06-18T21:17:06.890

You can try contacting the authors... I couldn’t find it on Google. – JayCe – 2018-06-18T22:30:47.147

Answers

6

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).

MD XF

Posted 2018-01-15T22:30:15.897

Reputation: 11 605

1Talk about the right tool for the job. – eaglgenes101 – 2018-06-02T03:56:19.177

8Have 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? – Engineer Toast – 2018-06-04T20:29:13.973

@EngineerToast Four years? How? – user202729 – 2018-06-08T09:53:51.810

@EngineerToast well, I only posted/accepted it since there were no other answers. – MD XF – 2018-06-18T21:11:49.540

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. – Engineer Toast – 2018-06-19T14:43:35.290

2

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)

RootTwo

Posted 2018-01-15T22:30:15.897

Reputation: 1 749

I think this is a non-standard library, so you should mention the library name in the language name, for example "Python + kociemba". – user202729 – 2018-06-08T09:47:27.130

1

Coincidentally the method is exactly the same. / You need to mention the builtin name (meta consensus)

– user202729 – 2018-06-08T09:51:31.983

1@user202729 actually, not coincidentally. That link is how I found the library. – RootTwo – 2018-06-08T17:09:22.593