Your task is to solve a new puzzle called Cubax. This toy is actually a Chinese puzzle whose expected “final” state is a simple cube. During the festivities, Cubax unfolded, because it cannot move when folded.
Cubax is composed of
N³ smaller cubes (elements) linked together in a kind of doubly linked chain. The chain has two ends, it doesn't loop.
3 successive elements of Cubax are either:
- always aligned
- always at a right angle (90°)
Thus a simple way of defining Cubax's structure is to provide the successive number of aligned blocks, starting from the first element.
For example, representing
S as the starting elements,
E as the ending element and
X as other elements, the structure defined as
332 could take any of the following 2D shapes:
SXX SXX SXX SXX
434 could be represented as:
Note: Counting the successive the number of aligned elements implies that elements in an angle are counted twice. In the last example
434, there are actually only
To provide the output solution, use the following rules.
For each series of aligned blocks, one character representing the direction where it has to point to is written:
UDirection Up (increasing y)
DDirection Down (decreasing y)
RDirection Right (increasing x)
LDirection Left (decreasing x)
FDirection Front (increasing z)
BDirection Back (decreasing z)
Considering a 3D representation (x,y,z), we will only be interested in the solutions where the first element is located at
(1,1,1) and where the last element is located at
Note: As a consequence, there will be no valid solution starting by
For the input
3332, the output
RDRD corresponds to the following planar shape:
For the same input
3332, the output
RDLU corresponds to:
Input & Output
Your program has to read the initial shape of Cubax and provide the moves to apply so as to pack it back to a full cube of side length
You are given an integer
N, the side length of the cube, and a string that contains a string of Blocks -> each character providing the successive number of aligned blocks
You are to output the alphabetically-sorted list of all solutions, one solution per line
# 2*2*2 cube
# 3*3*3 cube
# 4*4*4 cube
You are allowed to output the moves in a string or list/array
You are allowed to input the aligned blocks numbers via list/array as well
You may assume there will always be at least 1 solution to the puzzle
You are allowed to take in the input as 2 lines or on the same line as a list/array/string representation
Credits to the codingame community for the puzzle!
This is code-golf, so shortest code wins!