r/dailyprogrammer • u/Elite6809 1 1 • Apr 08 '14
[4/9/2014] Challenge #157 [Intermediate] Puzzle Cube Simulator
(Intermediate): Puzzle Cube Simulator
You may be aware of puzzles such as the Rubik's Cube. They work by having pieces with coloured faces which can rotate around the centers. You may also be aware of higher-order puzzles such as the Professor's Cube. These work in exactly the same way, with the exception of having more pieces. For the purposes of this challenge, an n-cube is a puzzle with n pieces along an edge - the Rubik's cube would be a 3-cube, and the Professor's cube a 5-cube.
To make it easier to see exactly what people are doing, there is a standard set of what is called Move Notation, which tells you exactly how the puzzle was turned. For the purpose of this challenge, the notation defined in Article 12 of the WCA regulations will be used. In a nutshell:
- There are 6 faces. U (up, the top face). D (down, the bottom face). L (left). R (right). F (front). B (back).
- Each face is turned like you were looking at it from the front.
- A notation such as
Xmeans you turn the X face clockwise 90'. SoR Lmeans turn the right face clockwise 90' (from its perspective), then the left face clockwise 90' (from its perspective). - A notation such as
X'(pronounced prime) means you turn the X face anticlockwise 90'. SoR U'means turn the right face clockwise 90', then the top face anticlockwise 90'. - A notation such as
X2means you turn the X face 180'.
This lets you signify a sequence of moves, such as R U R' U' R' F R2 U' R' U R U R' F' - which lets you know exactly what happened to the puzzle.
Your challenge is, given a 3-cube (the standard cube) and a sequence of moves, to simulate the turning of a puzzle and print the output state at the end. (you don't have to solve it - phew!)
Assume a standard colour scheme. That is, start with white on the bottom (D), yellow on the top (U), red on the front (F), green on the right (R), orange on the back (B) and blue on the left (L).
Formal Inputs and Outputs
Input Description
You will be given, on one line (and separated by spaces), a sequence of moves in WCA standard notation. This will be arbitrarily long, within sensible limits.
Output Description
You must print out the front face only of a cube that has been turned in the way described by the input (as if you were looking at it from the front of the cube.) Each colour will be represented by its first letter (r, o, y, g, b, w) and the face shall be represented as a printed square.
For example:
rrb
rrw
oww
Sample Inputs & Outputs
Sample Input
U2 R' D2 R F L' U2 R
Sample Output
rrb
rrw
oww
Challenge
Challenge Input
R U2 F2 D' F' U L' D2 U2 B' L R2 U2 D
Challenge Output
bbo
yrb
oow
Hint
Multidimensional arrays will be useful here. Try to visualise the way pieces are moved around when you turn a face.
3
u/badgers_uk Apr 09 '14
Python 3. Feels a bit brute-forcey because I just worked out the combinations of where the squares go for each rotation, (going to look up multi-dimensional arrays now!), but it works well enough.
3
u/OffPiste18 Apr 09 '14 edited Apr 09 '14
Here's a Scala solution.
Instead of going with a three-dimensional array and managing the cube as sub-cubes, I store the cube as six two-dimensional arrays and just deal with faces. Also, rather than trying to implement each of the possible rotations, which seemed pretty tricky and error-prone, I considered each one as three steps:
For each instruction, I flatMap to the appropriate three functions (each Cube => Cube), and just fold left!
Output matches the two sample test cases exactly.
3
u/iomanip1 Apr 09 '14 edited Apr 10 '14
EDIT: Python 2.7.4
Solved using numpy. I used a 5x5x5 array ('layered' outside the 3x3x3), representing the colors (or the colored stickers on a rubics cube, if you will), so that the colors rotate with the side. All rotations are carried out on the two outmost layers, thereby moving the colors together with the 'blocks' (the 3x3x3 array inside).
EDIT: fixed indentation, sublime text 2 is wonderful (http://www.sublimetext.com/), found it through this thread. Highly recommendedfor python!
5
u/skeeto -9 8 Apr 09 '14 edited Apr 09 '14
C++11.
I'm not getting theexactsame output, but the pattern is similar. Maybe I got one of the rotations wrong.Edit: I found the mistake (front rotation). The output is correct now.Each face is a separate 3x3 array and these faces provide row iterators for walking across any row of the face in any direction. This allows for a generic row rotation function that doesn't need to know exactly which rows it's managing. I didn't work out the counter-clockwise rotations, instead just rotating clockwise 3 times.
Output:
2
u/lukz 2 0 Apr 11 '14
vbscript
This challenge required good deal of time to think about how to store the piece colors and how to shuffle data upon rotation. Nice geometric problem!
Hint
2
u/ethnicallyambiguous Apr 11 '14 edited Apr 11 '14
My first submission here. Python 2.7.6 Criticism is appreciated (other than "why aren't the last two parts functions?")
2
u/matt_9k Apr 14 '14 edited May 08 '14
Python 2.75. A bit on the hardcoded side, but it gets the job done. Any feedback would be greatly appreciated.
1
u/dont_press_ctrl-W Apr 09 '14
I'm not sure what the perspective of a face means. Do you mean it would look clockwise if seen from the inside of the cube?
If the only movement applied to the fresh cube is 'U', is the result
namely clowise while looking towards the cube
or
namely clockwise while looking from inside the cube?
1
u/rsludge Apr 09 '14
This site may be useful for debugging http://ruwix.com/online-rubiks-cube-solver-program/
1
u/dohaqatar7 1 1 Apr 17 '14
I'm a bit stumped on this one. I'd love it if someone would post a nice java solution that I could read through and learn something from.
1
u/CodingFrisson Jun 17 '14
Finally solved! This took me some time and I even had to make myself a paper cube. Followed OffPiste18's plan to rotate the cube and turn only the front side. I'm a student, so any ideas on how to improve my programming skills and code are more than appreciated!
C++ (http://pastebin.com/15r7TKH9)
0
u/[deleted] Apr 13 '14
[deleted] — view deleted comment