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:
XE EX
X X
SXX SXX SXX SXX
X X
EX XE
Another example: 434
could be represented as:
S E
X X
X X
XXX
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 4+3-1+4-1=9
elements.
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:
U
Direction Up (increasing y)D
Direction Down (decreasing y)R
Direction Right (increasing x)L
Direction Left (decreasing x)F
Direction Front (increasing z)B
Direction 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 (N,N,N)
Note: As a consequence, there will be no valid solution starting by D
, L
or B
.
Example
For the input 3332
, the output RDRD
corresponds to the following planar shape:
SXX
X
XXX
E
For the same input 3332
, the output RDLU
corresponds to:
SXX
E X
XXX
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 N
.
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
Test cases:
# 2*2*2 cube
2
2222222
->
FRBULFR
FUBRDFU
RFLUBRF
RULFDRU
UFDRBUF
URDFLUR
# 3*3*3 cube
3
232333232223222233
->
FRFLUBRFDRBLBRDRUF
FUFDRBUFLUBDBULURF
RFRBULFRDFLBLFDFUR
RURDFLURBULDLUBUFR
UFUBRDFULFDBDFLFRU
URULFDRUBRDLDRBRFU
# 4*4*4 cube
4
3232242323232222222233222223422242322222222242
->
FRBULFDRBUBDBRFUFDFULBUBRDRULDFURDFLULDBLFUBRF
FUBRDFLUBRBLBUFRFLFRDBRBULURDLFRULFDRDLBDFRBUF
RFLUBRDFLULDLFRURDRUBLULFDFUBDRUFDRBUBDLBRULFR
RULFDRBULFLBLURFRBRFDLFLUBUFDBRFUBRDFDBLDRFLUR
UFDRBULFDRDLDFURULURBDRDFLFRBLURFLUBRBLDBURDFU
URDFLUBRDFDBDRUFUBUFLDFDRBRFLBUFRBULFLBDLUFDRU
Others:
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!
Scoring
This is code-golf, so shortest code wins!