11
\$\begingroup\$

A cube has 6 faces. We can define it in terms of triangles only, by splitting each square face on the diagonal.

Each vertex of the cube is numbered 0 through 7. The coordinates of a vertex are that vertex's number encoded in binary. For example, the coordinates of vertex 5 are (1,0,1).

Requirements:

Your program must output a list of 12 triangles, where each triangle is named with its three vertices. For example, one valid output triangle is (2,3,6).

You can use whatever kind of delimiting you want between vertices (or none), but there must be a different delimiter between the vertices of a triangle, and between triangles. A minimal output might look like: 236 645 510...

The vertices of a given triangle may be listed in any order. The faces of the cube may each be split along either of their diagonals (but both triangles of a given face must share a diagonal!). No triangle may be repeated (including by listing the same triangle with multiple vertex orderings - if you output 623, you may no longer output 236).

Output must be ASCII numerals 0-7, and whatever delimiters you choose.

Standard code golf rules apply.

New contributor
TJM is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Related \$\endgroup\$
    – Arnauld
    Commented yesterday

12 Answers 12

9
\$\begingroup\$

JavaScript (ES6), 50 bytes

Returns a list of 12 triplets.

f=n=>n>11?[]:[[x=~~n%6+1,3*x%7,n>5?7:0],...f(-~n)]

Try it online!

Triangles

This generates the following triangles:


JavaScript (ES6), 48 bytes

This version returns the list of 12 integers found by xnor, but sorted from lowest to highest and delta-encoded.

A fun thing in there is that 14 is used twice, in two different bases: once as decimal and once as hexadecimal.

_=>[..."DEE26966638",t=14].map(n=>t+=("0x"+n)*7)

Try it online!

Or 42 bytes for a more direct port in Node.js.

\$\endgroup\$
2
  • \$\begingroup\$ 047 is not a triangle on a face of the binary labeled cube. \$\endgroup\$
    – Tbw
    Commented yesterday
  • \$\begingroup\$ @Tbw Thanks for reporting this. Should be fixed now. \$\endgroup\$
    – Arnauld
    Commented yesterday
7
\$\begingroup\$

Ruby, 36 bytes

As noted by GB a port of the approach used by Arnauld and Lucenaposition is shorter than my original answer.

12.times{|i|p [i/6*7,k=1+i%6,k*3%7]}

Try it online!

Ruby, 38 bytes

My original answer, which was posted before Lucenaposition and shorter than Arnauld's at the time of posting. It's also nicely algorithmic.

j=2
12.times{|i|p [j,j^=4>>i%3,i/6*7]}

Try it online!

The triangles are per the unfolded cube net below. For the last point in each of the 12 triangles, we output 6 points with polar vertex 0 then 6 points with polar vertex 7.

For the other two points in the triangle we go round 6 vertices of the equator twice, changing one bit per iteration. on each iteration we output the before and after points, which are the other two vertices of our triangle.

1--3--7
| /| /|
|/ |/ |
0--2--6--7
   | /| /|
   |/ |/ |
   0--4--5--7
      | /| /|
      |/ |/ |
      0--1--3  
\$\endgroup\$
2
  • \$\begingroup\$ 36 bytes - 12.times{|i|p [i/6*7,k=1+i%6,k*3%7]} \$\endgroup\$
    – G B
    Commented 22 hours ago
  • \$\begingroup\$ @GB thanks, edited in with credit to yourself, Arnauld and Lucenaposition. \$\endgroup\$ Commented 11 hours ago
5
\$\begingroup\$

Python, 35 bytes

for c in b"+NQ<B-3HYm":print(c*7)

Try it online!

Outputs the triangles hardcoded as three-digit numbers.

301
203
546
567
420
462
315
357
105
504
623
763

The bytestring holds numbers from 0 to 127 encoded as ASCII characters, of which two are unprintable. We multiply each value by 7 to reach more three-digit numbers. Fortunately, multiples of 7 suffice to represent each triangle, because there's enough choices in splitting each face and ordering each triangle's vertices.

\$\endgroup\$
3
  • \$\begingroup\$ As delimiters are free, this solution saves 5 bytes: [c*7 for c in b"+NQ<B-3HYm"] \$\endgroup\$ Commented 11 hours ago
  • \$\begingroup\$ After digging a bit from what you did, I found a solution in 28 bytes: set(map(ord,'ĭËȢȷƤǎĻťiǸɯ˻')) \$\endgroup\$ Commented 11 hours ago
  • \$\begingroup\$ Although I'm sure we can do petter by finding a valid permutation of triangles and vertices that we can concatenate to get a number that can be written as AB**CD (A,B,C,D being single digits), allowing us to write the 13 bytes f"{AB**CD:_}" which would display our triangles coordinates separated by underscores (as the number will be displayed with an underscore every 3 digits). \$\endgroup\$ Commented 10 hours ago
4
\$\begingroup\$

J, 21 bytes

'31204153726475'[\~3:

Try it online!

Produces all 12 triangles from one strip (taking every 3 consecutive digits from the string).

[ is the identity function; \ is used to apply it to infixes of equal length; ~ swaps arguments – this rearrangement is to allow the whole thing to be a verb (which ignores its argument) rather than a constant value.

\$\endgroup\$
4
\$\begingroup\$

bytes

“Ɠæ,ṖG’b8ṡ3

A niladic Link that yields a list of the twelve triples.

Try it online!

See the order visually on Desmos.

How?

“Ɠæ,ṖG’b8ṡ3 - Link: no arguments
“Ɠæ,ṖG’     - 578487238572
       b8   - convert to base eight
                -> [1,0,3,2,6,0,4,1,5,3,7,6,5,4]
         ṡ3 - overlapping slices of length three

I also had a search to see if I could use a fourteen-letter word from Jelly's dictionary for something like this for 10 bytes:

“¬Ṛṡ»O%8ṡ3 - Link: no arguments
“¬Ṛṡ»      - "hydrocephaloid"
     O     - ordinals -> [104,121,100,114,111,99,101,112,104,97,108,111,105,100]
      %9   - mod 9 -> [5,4,1,6,3,0,2,4,5,7,0,3,6,1]
        ṡ3 - overlapping slices of length three

That was the only list I could find that had two of each of six of the values \$[0,7]\$ (using %8, %9, %⁵ (\$10\$), and %⁴ (\$16\$)) and one each of the other two such that all slices of five are composed of five different values, but it still doesn't have all the properties it needs to make it produce a valid triangle walk. There are some others like this using thirteen-letter words with a leading space, like " contraception" (“ı2ġ») with %8, but none of those are valid either.

\$\endgroup\$
3
\$\begingroup\$

Uiua 0.15.0-dev.2, 20 bytes SBCS

°⋯♭₃≡⊞↻¤⇡3⋯⊂+4.⧈∘3⇡4

Try on Uiua Pad!

A full program that outputs an 12×3 array. I bet there is some more clever arithmetic way to do this.

Explanation

♭₂°⋯≡⊞↻¤⇡3⋯⊂+4.⧈∘3⇡4
                     ⇡4 # 0_1_2_3
                 ⧈∘3   # 3-size windows: [0_1_2 1_2_3]
             ⊂+4.      # join with that array plus 4, top and bottom faces
           ⋯           # to binary
     ≡⊞↻¤⇡3            # on each row, construct the table of all three rotations of its rows
  °⋯                   # from binary
♭₂                      # flatten into rank 2 array
\$\endgroup\$
3
\$\begingroup\$

JavaScript (V8), 42 bytes

for(x=y=12;x--;)print(y%7,y=y*3%7,x>5?7:0)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 26 16 bytes

F⁶E07⁺κ⭆²﹪X³⁺ιμ⁷

Try it online! Link is to verbose version of code. Explanation: Now uses @Lucenaposition's observation that the equator is the powers of 3 modulo 7.

F⁶                  Loop `6` times and print
   07               Literal string `07`
  E                 Map over characters
        ²           Literal integer `2`
       ⭆            Map over implicit range
           ³        Literal integer `3`
          X         Raised to power
             ι      Outer value
            ⁺       Plus
              μ     Inner value
         ﹪          Modulo
               ⁷    Literal integer `7`
     ⁺              Concatenated to
      κ             Current character
\$\endgroup\$
1
\$\begingroup\$

Python 3, actually calculated 59/104 bytes

104 bytes (for 1 digit n), can do any number of dimensions:

n=3
R=range
for i in R(2**n):bin(i).count('1')%2or print(*[(i,i^2**j,i^2**k)for k in R(n)for j in R(k)])

59 bytes, 3 dimensions only:

for i in 0,3,5,6:print((i,i^1,i^2),(i,i^1,i^4),(i,i^2,i^4))

we take the triangles "rooted" (with corner opposite hypotenuse) in corners whose bitsum is even. this guarantees we cover the whole shape without overlap (in any number of dimensions)

to move along any given axis we just bitwise xor with a power of 2 corresponding to it

New contributor
3RR0R404 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
0
\$\begingroup\$

Zsh, 41 bytes

for i (13 26 32 45 51 64)echo $i\0\\n$i\7

Try it online!

Generates the list found by Arnauld.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Can't you save two bytes with 0$i and 7$i? \$\endgroup\$
    – Neil
    Commented 19 hours ago
0
\$\begingroup\$

Bash, 46 bytes

echo 03{1,2} 27{3,6} 06{2,4} 05{1,4} 47{6,5} 17{5,3}

Explanation: I followed Arnauld's diagram . For each cube's face in the diagram, I took the diagonal drawn there, and the two remaining vertices of that face.

I admit, there is nothing smart in this solution. Instead of actually using code to generate the solution, I use code to merely output a pre-computed "paper-based" solution.

New contributor
fast ethernet is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
0
\$\begingroup\$

Maple, 68 bytes

proc(p)w:=[0,p[1],add(p)];w,7-~w end~(combinat:-permute([1,2,4],2));

Outputs [[0,1,3],[7,6,4],[0,1,5],[7,6,2],[0,2,3],[7,5,4],[0,2,6],[7,5,1],[0,4,5],[7,3,2],[0,4,6],[7,3,1]]

Calculates the vertices, based on the following observation. Starting from the binary coordinates [0,0,0] we find the 6 triangles including this vertex have binary coordinates [0,0,0], [0,0,0] with 1 bit flipped, and [0,0,0] with that bit and also a second bit flipped, e.g., [0,0,0],[0,1,0],[0,1,1]. Thinking in bit-reversed order, we can get these by 0, add 1, add 1 and add 4 or triangle [0,1,5].

The other 6 triangles can be obtained by subtracting each vertex from 7 (corresponding to the triangles including coordinates [1,1,1], and just flipping all bits), e.g., [0,1,5] becomes [7,6,2].

Ungolfed version:

u:=combinat:-permute([1,2,4],2); # gives [[1,2],[1,4],[2,1],[2,4],[4,1],[4,2]];
q:=proc(p)                       # takes one of above, e.g., [1,4]
     w:=[0,p[1],add(p)];         # w := [0, 0+1, 0+1+4] = [0,1,5]
     x:=7-~w;                    # x := [7-0, 7-1, 7-5] = [7,6,2]
     w,x                         # output both
   end;
q~(u);                           # map q over u

Probably I should have abandoned the algorithmic approach for one using predefined vertices; after all just writing the output takes only 48 bytes. But the question does instruct us to "generate" the indices :-). Maple is not very efficient with bit manipulations, but the bit manipulations might be more efficient in another language.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.