25
2
A venerated pass time of pedants is to point out that pictures of "Rubik's Cubes" (on t-shirts, posters etc.) are not actually solvable.
The first thing that should be checked is that the cube is made up of the right pieces. To be solvable a cube needs six colors each with nine squares. The cube also needs each edge and corner unit (these are the smaller cubes that make up the cube) to be unique. Not only must they be unique but if two center pieces are opposite each other no edge or corner piece can contain both of those colors.
Once you have a cube that is made up of all the right pieces you still need to verify it can be solvable. There are a couple of rules here, so I'll defer to an expert to explain them, the spoiler below explains how we can do this. If you are interested in solving the problem on your own you don't need to visit the site to understand or participate in this challenge.
Your task is to take a pattern as input and determine if it is in fact a solvable Rubik's cube. To be solvable there must be a way to perform valid moves on a cube so that the cube has only one color on each face (and the different faces have different colors). Most Rubik's cubes have a standard coloring (White is opposite Yellow, etc.) you may not assume that the solve state follows this particular coloring.
A valid move is either the clockwise or anti-clockwise rotation of a single face of the cube. With the rotation of the face of the cube any squares bordering the face are rotated as well, staying connected to the face they were previously touching.
IO
You may take the cube in any reasonable manner. If your language has some builtin "cube-face" type, good for you, that is fine as input, other wise you can take a 2D array of the net, of the cube, 1 3 by 3 lists for each face. Just be reasonable. If you want to know if a specific format is acceptable comment or ping me in chat and I will add to the challenge to state its validity.
Your input format need only support up to 9 possible colors.
For output this is a decision problem so you should output one constant value for "Yes, this is a valid Rubik's cube" and one different constant value for "No, this is not a valid Rubiks cube".
This is code-golf so answers will be scored in bytes with less bytes being better.
Test Cases
Here are test cases. They are formatted as the net of a cube with each square as a single letter. Different letters represent different colors. Any more testcases can be added upon request.
Solvable
RRR
RRR
RRR
GGGWWWBBBOOO
GGGWWWBBBOOO
GGGWWWBBBOOO
YYY
YYY
YYY
GRR
GRR
ORW
WWRBWYBOOGGY
GGRBWGYBBOOO
OOGRWGYWWRBB
WYO
YYB
YYB
Unsolvable
RRR
RRR
RRR
GGGWWWBBBOOO
GGGWWWBBBOOO
GGGWYWBBBOOO
YWY
YYY
YYY
RRR
RRR
RRR
GGGWWWBBBOOO
GGGWWWBBBOOO
GGGWWWBBBOOO
YWY
YYY
YYY
RRR
RRR
GGG
GGYWYWRBBOBO
GGYWWWROBOOO
GGYWWWRBBOOO
BBB
YWY
YYY
RRW
RRW
GGG
GGYWWYEOBROO
GGYWWYEBBROO
GGOWWYWBBROO
BBB
YYW
YYO
15I feel obligated to point out that the Rubik's cube in your avatar is not solvable. It only has 4 squares on the side facing us, whereas a normal Rubik's cube should have 9. Not to mention the weird symbols on top of the squares. – James – 2017-12-21T21:04:07.793
2@DJMcMayhem My children have Rubik's cubes with only four "sub-cubes". – Adám – 2017-12-21T21:06:00.013
Can we assume a standard colouring? e.g (W opposite Y ...) – H.PWiz – 2017-12-21T21:06:22.150
2@H.PWiz No, you can't. That was implicit in my definitions but I will make it explicit in the question. – Post Rock Garf Hunter – 2017-12-21T21:09:05.103
1@DJMcMayhem By "normal", you mean 3×3. :P That could be a 2×2 cube. The symbols are definitely unlike a Rubik's cube, though. – totallyhuman – 2017-12-21T21:38:13.723
Can we take input like this (with 0 representing R, 1 representing G, etc)?
– MD XF – 2017-12-22T00:09:44.857@MDXF Yes sure. – Post Rock Garf Hunter – 2017-12-22T00:27:10.307
@WheatWizard Thanks (not sure if it will actually help but might make it easier for Cubically)
– MD XF – 2017-12-22T00:28:29.5672
Your specification does not include a complete description of the three parity laws of the cube. 1. It is impossible to have only 1 edge flipped 180 degrees (mentioned) 2. It is impossible to have only 1 corner twisted 120 degrees (not mentioned) 3. It is impossible to have an odd permutation of the cubies (not mentioned.). I'm casting a close vote until this is resolved. See https://www.ryanheise.com/cube/cube_laws.html for an explanation.
– Level River St – 2017-12-22T03:15:31.607@LevelRiverSt I added the link to the site you linked. I will also point out that my original explanation did hit point 2. – Post Rock Garf Hunter – 2017-12-22T05:25:50.850
2
@DJMcMayhem You're referring to this, right? Just keeping it in case OP changes their avatar.
– Erik the Outgolfer – 2017-12-22T09:48:28.393@Adám This is 3D, so a 2×2×2 has 8 sub cubes. Unless you're talking about a 2×2×1... Anyway - how is this unclear? – user202729 – 2017-12-22T10:36:02.023
4@LevelRiverSt Note that the Rubik's cube is self-contained, anyone can derive at the mathematical formulation and parity laws independently. – user202729 – 2017-12-22T10:40:50.690
@user202729 My comment was an answer to DJMcMayhem's. – Adám – 2017-12-22T12:44:10.290
1
@user202729 While it's possible to derive the parity laws independently, they're not obvious and it's likely some might miss them. Further, the original post pointed out edge flip parity but not swap parity, which might lead one to think swap parity could be ignored. Meta question https://codegolf.meta.stackexchange.com/q/14383/15599 is precisely about these occasions when the spec does not match reality, whether the OP did it intentionally to simplify the challenge or unintentionally as they had an incorrect understanding. Hence the need for disambiguation. I've now withdrawn my close vote.
– Level River St – 2017-12-22T18:07:06.5802@WheatWizard Thanks your intent is clearer now. Your original spec discussed swapping 2 stickers on the same cubie. This has the same effect as flipping an edge. But when 2 stickers are swapped on a corner it transforms into its mirror image, making the cube unsolvable for geometric (not parity) reasons even if dismantled. This is quite different from rotating a single corner 120 degrees. In that case the cube can be solved without peeling if dismantled, but cannot be solved by normal moves for parity reasons. The latter is harder to detect and I don't think it is covered in your original spec – Level River St – 2017-12-22T18:26:58.867
1@trichoplax Thanks, fixed now. – Post Rock Garf Hunter – 2017-12-24T18:38:51.513
@WheatWizard can input be a 3x2 matrix of 3x3 matrices, arranged as row0: front back, row1: left right, row2: up down? – ngn – 2018-01-11T08:03:26.493
@ngn of course it can. As long as you are not trying to use the input to encode extra information you are fine. – Post Rock Garf Hunter – 2018-01-11T14:20:00.417
@EriktheOutgolfer Thanks for keeping the image for posterity. OP's avatar is now a frog. A frog is most certainly not a solvable cube. – Captain Man – 2018-01-24T14:29:25.350