19
1
Assign the numbers 0 through 7 to the 8 vertices of a cube in any way you want. Exactly one number must be assigned to each vertex.
For example, your vertices might be assigned like this:
3-----1
/| /|
4-----2 |
| | | |
| 5---|-0
|/ |/
6-----7
Write a program that takes in an integer from 0 to 5. Each of these 6 numbers is associated with exactly one face of your cube in any way you like. When one of these numbers is input, the 4 vertex numbers of the associated face must be printed to stdout in a 2×2 square of digits. The face is to be viewed straight on from outside the cube. All 4 face rotations are valid.
For example, if 0 is associated with the front face of the example cube above, then this would be a valid output for input 0
:
42
67
The face may be viewed at any 90° rotation, so these are also valid:
27
46
76
24
64
72
This output (and its rotations) are not valid, as they are viewed from the wrong side of the face:
24
76
The same idea applies to all other faces. e.g. if 1 is associated with the back face, then input 1
might produce output 13[newline]05
(and 31[newline]50
would be invalid).
So the real challenge is choosing your vertex numbers and rotations such that translating the input into its 4 vertex numbers is easy and short.
The shortest code in bytes wins. Tiebreaker is earlier post. (Handy byte counter.)
Notes
- You may write a function instead of a program. It should take an integer from 0 to 5 and print or return the 2×2 digit grid string.
- Take input from stdin, command line, or function arg. You may assume input is valid.
- The output may optionally have a trailing newline.
- Be sure to tell us the vertex and face numbers you chose.
2I tried permutations as an elegant mathematical approach and it's more verbose than hard-coding without your optimisations. – Peter Taylor – 2015-04-14T17:56:15.530
Your latest answer is brilliant. I think you should have posted it as a separate answer as it's a completely different approach and worthy of another upvote. Basically you have the same cube as my C answer but with the first three even corners shifted one place. I can't believe I missed
6+n%2 --> 6|n
(i've already incorporated that into my Ruby answer.) Note that by performing the transformn --> n^1
on the faces you could simplify your formulas, though I'm guessing that as you discardn
and carry on withn^1
it won't help your score. – Level River St – 2015-04-15T17:16:13.173@steveverrill Thanks for the praise! I asked in chat if I should post this as an entirely new answer, but there wasn't a consensus so I didn't. I was definitely pleased with myself when I realized that carefully ordering the
n
andn^1
pairs around the cube would allow me to calculate another vertex with just|6
. And I didn't see thatn --> n^1
transform, which definitely makes sense. But you correctly surmised that it wouldn't actually affect my score, so I'll probably just leave it as-is. – Runer112 – 2015-04-15T17:32:28.370I've gone ahead and incorporated your XOR idea into my Ruby answer. It gives a saving of 10 (in addition to the 2 for
6+n%2 --> 6|n
) I hope you don't mind. I did use then --> n^1
transform on the faces, so my latest revision gives the same outputs as yours, but with different inputs. BTW, I don't think bit operations are inelegant, it all depends how you use them! – Level River St – 2015-04-15T18:04:39.33011 char shorter in GolfScript:
~.1^..n@6|@3+6%
– Peter Taylor – 2015-04-15T18:56:15.370@PeterTaylor It's been so long since I've used GolfScript that I forgot about the wonderful feature that input is automatically put on the stack. Good catch. I think a couple of people have asked aditsu to modify CJam to implicitly read the input when an attempt is made to read from the empty stack. Unfortunately, nothing has come of this yet. – Runer112 – 2015-04-15T19:01:12.887