# Solve the Alien Probe puzzle

## Introduction

In this TED-Ed video (which I'd recommend watching first), a puzzle is presented. 27 unit cubes must be painted with one of red, green, or purple on each face, so that they can be reassembled into a large 3x3x3 cube that shows only red on the outside, then a different arrangement that shows only green, and then only purple. One solution is presented at the end of the video, but it is not obviously generalisable (and there are lots more solutions).

Then, in this lazykh video, a simple algorithm for solving the puzzle is presented. It is as follows:

1. Paint the faces that currently make up the exterior of the large cube green
2. For each dimension of the cube (X, Y, Z), shift the arrangement of the cubes over in that direction by 1 unit
3. Repeat these steps for each colour

The lazykh video has graphics which explain the algorithm more clearly than can be done in this textual medium, so I suggest you watch that as well.

This algorithm generalises easily for higher sizes and numbers of colours than 3 (e.g. a 5x5x5 cube which must be painted with 5 different colours). Interestingly, it also generalises to higher dimensions than 3, but I'll save that for a different challenge.

## Challenge

Given the size $$\ n \$$ (where $$\ n \$$ is a positive integer) of the large cube, output a solution to the $$\ n \times n \times n \$$ version of this generalised puzzle.

Using the algorithm described above is certainly not the only way to find a solution, but I've included it as a starting-point. You are free to work out the solution however you choose.

Output should be given as a list of 6-tuples (or similar structure), representing the face colours of each small cube. The elements of the tuples should be from a predictable sequence of distinct values, (e.g. integers from $$\ 0 \$$ to $$\ n - 1 \$$), each representing a colour. The faces within a tuple can be in whatever order you choose but it must be consistent. The list should be flat and $$\ n ^ 3 \$$ in length, but its elements can be in any order (since they will need to be rearranged anyway for assembly into the $$\ n \$$ alien probes).

## Test cases

Input Output
1     [(0, 0, 0, 0, 0, 0)]
2     [(0, 0, 0, 1, 1, 1), (1, 0, 0, 0, 1, 1), (0, 1, 0, 1, 0, 1), (1, 1, 0, 0, 0, 1), (0, 0, 1, 1, 1, 0), (1, 0, 1, 0, 1, 0), (0, 1, 1, 1, 0, 0), (1, 1, 1, 0, 0, 0)]
3     [(0, 0, 0, 2, 2, 2), (2, 0, 0, 1, 2, 2), (1, 0, 0, 0, 2, 2), (0, 2, 0, 2, 1, 2), (2, 2, 0, 1, 1, 2), (1, 2, 0, 0, 1, 2), (0, 1, 0, 2, 0, 2), (2, 1, 0, 1, 0, 2), (1, 1, 0, 0, 0, 2), (0, 0, 2, 2, 2, 1), (2, 0, 2, 1, 2, 1), (1, 0, 2, 0, 2, 1), (0, 2, 2, 2, 1, 1), (2, 2, 2, 1, 1, 1), (1, 2, 2, 0, 1, 1), (0, 1, 2, 2, 0, 1), (2, 1, 2, 1, 0, 1), (1, 1, 2, 0, 0, 1), (0, 0, 1, 2, 2, 0), (2, 0, 1, 1, 2, 0), (1, 0, 1, 0, 2, 0), (0, 2, 1, 2, 1, 0), (2, 2, 1, 1, 1, 0), (1, 2, 1, 0, 1, 0), (0, 1, 1, 2, 0, 0), (2, 1, 1, 1, 0, 0), (1, 1, 1, 0, 0, 0)]
6     https://gist.github.com/pxeger/a82bdce4427f487490ce0df19840807a#file-6-txt
20    https://gist.github.com/pxeger/a82bdce4427f487490ce0df19840807a#file-20-txt


## Rules

• Your solution does not need to complete in reasonable time for large $$\ n \$$, but it must work in theory
• Standard loopholes are forbidden
• You may use any sensible I/O method
• This is , so the shortest code in bytes wins
• Sandbox Apr 28, 2021 at 15:49
• fun fact, if you interpret the tuples for n=2 as a binary number you get the first 8 multiples of 7, whether or not this is useful, idk Apr 28, 2021 at 16:38

# Wolfram Language (Mathematica), 363433 30 bytes

#-1|#&~Array~#~Tuples~3/.#->0&


Try it online!

Returns a length-$$\n^3\$$ list of $$\\{x|x',y|y',z|z'\}\$$ colorings.

Verify with a $$\n\times n\times n\$$ matrix output.

Explanation:

Consider a cube $$\x\$$th from the front in some initial configuration. If the frontmost layer of cubes is painted $$\1\$$, the second $$\2\$$, etc., so that the color number increments with each shift, the front face of the cube will be painted $$\x\$$. After one more shift, the cube will be in the backmost layer, so the back face is painted $$\x+1\$$.

The same logic applies in other dimensions, so we can paint the cube at position $$\(x,y,z)\$$ with colors $$\(x,y,z,x+1,y+1,z+1)\$$ (modulo $$\n\$$).

#-1|#&~Array~#~Tuples~3         (* 3-tuples of {0|1, 1|2, ..., n-1|n} *)
/.#->0&  (* and replace n with 0. *)

• This looks clever, any chance of an explanation for those who don't speak mathematica? Apr 28, 2021 at 18:47

# Charcoal, 21 bytes

ＮθＦθＦθＦθＦ⟦ικλ⟧⭆²﹪⁺μνθ


Try it online! Link is to verbose version of code. Outputs the faces in order 162534 (relative to dice numbering). Explanation:

Ｎθ


Input n.

Ｆθ


Loop over the x-axis.

Ｆθ


Loop over the y-axis.

Ｆθ


Loop over the z-axis.

Ｆ⟦ικλ⟧


For each coordinate...

⭆²﹪⁺μνθ


... output it, then output it incremented modulo n.

• -3 bytes thanks to xnor.
f n=mapM(\_->zip<*>(n:)$[1..n])"..."  Try it online! The relevant function is f, which takes as input the integer n and returns a list of $$\n^3\$$ cubes; each cube is represented as a list of three pairs, such as [(1,2),(1,3),(2,3)]. Each pair represents the colors of two opposite faces, and colors are numbered from 1 to n. ## How? The strategy described by the OP is equivalent to the following. Consider the cube at coordinates $$\(x,y,z)\$$, with $$\1\le x,y,z\le n\$$. Paint the two faces orthogonal to the $$\x\$$ axis with colors $$\x\$$ and $$\x+1\$$ (considered $$\\mod n\$$). Similarly, color the faces orthogonal to the $$\y\$$ axis with colors $$\y\$$, $$\y+1\$$ and the faces orthogonal to the $$\z\$$ axis with colors $$\z\$$, $$\z+1\$$. Therefore, to generate the $$\n^3\$$ colored cubes, we just have to independently pick three pairs of colors from the set $$\{(1,2),(2,3),\ldots,(n-1,n),(n,1)\},$$ and this is exactly what the code above does. • 36 bytes – xnor Apr 28, 2021 at 21:10 # JavaScript (V8), 113 ... 108 95 bytes Saved 2 bytes thanks to @EliteDaMyth Saved 1 byte thanks to @Shaggy Prints the cubies. n=>{for(x=n**3;x--;print(a))for(a=[c=n];c--;)for(j=6;j--;)(x/n**(j>>1)%n+c)%n^j%2*~-n?0:a[j]=c}  Try it online! • you can replace (~~(x/n**(j>>1)%n)+c)%n with ~~(x/n**(j>>1)%n+c)%n to save 2 bytes according to my testing – user100752 Apr 28, 2021 at 17:05 • Could you save a byte by declaring c within a: a=[c=n]? Apr 28, 2021 at 19:11 # Ruby, 67 66 56 54 bytes ->n{(1..n**3).map{|y|(0..5).map{|x|(x+y/n**(x/2))%n}}}  Try it online! ### Explanation: • Look at the existing answers • Try to find a pattern in the results • Replicate the pattern without understanding the underlying process • Feels like cheating.but it works • +1 for your honesty Apr 29, 2021 at 15:03 # Jelly, 6 bytes ṗ3ż’$%


A monadic Link accepting a positive integer that yields a list. Each element is a pair of lists, the first gives the colours of three faces around a corner and the second gives the colours of their respective opposites.

Try it online!

### How?

The strategy is to paint each of the $$\n^3\$$ cubes in a consistent direction (e.g. always clockwise) around any corner with a different one of the $$\n^3\$$ possible $$\3\$$-tuples of the $$\n\$$ colours. Then chose any ordering of the colours and paint every unpainted face with the "next" colour after the painted face it is opposite. This gives the same resulting painting as the strategy given in the question, just in a different order.

ṗ3ż’$% - Link: integer, n 3 - three ṗ - (implicit range [1..n]) Cartesian power (3) -> all 3-tuples made from [1..n]$  - last two links as a monad - f(x=that):
’   -   decrement (x) (vectorises)
ż    -   (x) zip with (that)
% - modulo (n) (vectorises)


# R, 53 bytes

n=scan();cbind(e<-expand.grid(a<-1:n-1,a,a),(e+1)%%n)


Try it online!

Returns a matrix with rows corresponding to different small cubes, and columns - to their faces.

# 05AB1E, 9 bytes

L3ãεD>‚I%


Try it online!

L3ãεD>‚I%  # full program
ε       # replace each element in...
ã        # all lists of length...
3         # literal...
ã        # of elements in...
L          # [1, 2, 3, ...,
# ..., implicit input...
L          # ]...
ε       # with...
‚    # [...
# (implicit) current element in map...
‚    # , ...
D      # (implicit) current element in map...
>     # plus 1...
‚    # ]...
# (implicit) with each element...
%  # modulo...
I   # input
# (implicit) exit map
# implicit output