Implement the Cubically Cube

10

0

Cubically is a fairly new esoteric language capable of creating short, golf-like answers for a very specific subset of problems. It is unique in that it stores memory in the form of a 3x3 Rubik's cube, making calculations far less trivial than in most languages. In Cubically, the programmer must rotate the internal cube in order to manipulate the values stored on the faces, then use those values in their calculations. Calculations are performed on a single 32-bit integer stored on an imaginary face known as the "notepad". Additionally, Cubically can request user input and store it in an input buffer consisting of only a single integer value.

The Cube

The faces of the cube are Up, Down, Left, Right, Front, and Back:

   UUU
   UUU
   UUU
LLLFFFRRRBBB
LLLFFFRRRBBB
LLLFFFRRRBBB
   DDD
   DDD
   DDD

When the program starts, the cube is initialized such that each square on that face is equal to that face's 0-based index:

   000
   000
   000
111222333444
111222333444
111222333444
   555
   555
   555

Whenever a face is rotated, it is always rotated clockwise:

Cubically> F1

   000
   000
   111
115222033444
115222033444
115222033444
   333
   555
   555

The value of a face is defined as being the sum of every square on that face. For example, in the above cube, the value of face 0 is 3.

Syntax

Commands are executed by first loading a command into memory, then passing arguments to it to execute the command. For example, the command F1 will load the command F into memory, then call it with the argument 1. Additionally, F13 will load the command F into memory, then call it with the argument 1, then call it with the argument 3. Any non-digit character is treated as a command, and any digit is treated as an argument.

Your task

Your task is to implement Cubically's internal memory cube in a language of your choice. Your code should be able to execute a very small subset of the language.

Commands

  • R - Rotate the right face of the cube clockwise the specified number of times.
  • L - Rotate the left face of the cube clockwise the specified number of times.
  • U - Rotate the top face of the cube clockwise the specified number of times.
  • D - Rotate the bottom face of the cube clockwise the specified number of times.
  • F - Rotate the front face of the cube clockwise the specified number of times.
  • B - Rotate the back face of the cube clockwise the specified number of times.
  • % - Outputs the value on the given face. The value of a face is defined as the sum of all squares on that face.

Rules

  • You may use any language created before or after the date this challenge was posted to write a program or function capable of solving this challenge.
  • Input will be passed in either through STDIN, as a string, or as a character array (you choose, please specify).
  • Output must be passed either to STDOUT or as the output of the function, and it must be either an integer, a string containing only digits, or an array of digits. If your language requires you to output a trailing newline, you may do so.
  • The input will always be in the following format: ([UDLRFB]\d*)*%[0-5]. There will be no whitespace characters in the input.
  • The input for % will always use a 0-based index.

This is , so shortest answer in bytes wins.

Test Cases

%0 -> 0
%1 -> 9
%2 -> 18
%3 -> 27
%4 -> 36
%5 -> 45
R1%2 -> 27
RD3F2%5 -> 32
L1R23F1B5U9D2%3 -> 17

For more test cases, check out the TIO interpreter. If TIO isn't working, you may use the Lua interpreter instead.

TehPers

Posted 2017-08-08T21:51:35.177

Reputation: 899

3

Is this question a duplicate of this?

– TehPers – 2017-08-08T22:00:23.150

Related. – notjagan – 2017-08-08T22:01:11.480

@TehPers I think it's very similar. For future challenges, I recommend leaving them in the sandbox for at least 24 hours, longer if you still have questions about it. (I had to learn this the hard way; my first challenges were not well-received at all. This isn't closed or downvoted yet so it's not bad, but had it been left in the sandbox for longer, other users might've noticed flaws that I didn't.) – MD XF – 2017-08-08T22:46:31.260

@MDXF I'll keep that in mind for next time. Thanks! – TehPers – 2017-08-09T00:08:28.880

Did you miss out a multiple of 4 between R and D in the example RD3F2%5 -> 30? – Jonathan Allan – 2017-08-09T02:58:17.790

@JonathanAllan R is loaded into memory, but D overwrites it before it's called, meaning the R does nothing. Does this make sense now? – TehPers – 2017-08-09T03:12:37.520

Oh OK, yes that makes sense, it's just the description did not say (I see now that the regex has a * too anyway!). Thanks. – Jonathan Allan – 2017-08-09T03:15:04.943

@MDXF Oh, I'll fix that real quick. Thanks! – TehPers – 2017-12-01T02:09:58.930

Answers

8

Python 2, 476 bytes

import re
c=[[i]*9for i in range(6)]
r=lambda f:[f[a+b]for a in(0,1,2)for b in(6,3,0)]
U=lambda c:[r(c[0])]+[c[j+1][:3]+c[j or 4][3:]for j in(1,2,3,0)]+[c[5]]
y=lambda c:[r(c[0])]+c[2:5]+[c[1],r(r(r(c[5])))]
z=lambda c:[c[2],r(r(r(c[1]))),c[5],r(c[3]),c[0][::-1],c[4][::-1]]
s=input()
exec("c="+"(c);c=".join("".join("zzzUz U zzUzz yyyzUzzzy zUzzz yzUzzzyyy".split()[ord(t)%11%7]*sum(map(int,n))for t,n in re.findall("([BUDLFR])(\d*)",s[:-2])))+"(c)")
print sum(c[int(s[-1])])

Try it online!

A port from my Simulate A Rubik's Cube answer. The revisit prompted me to golf that by 47 bytes.

Jonathan Allan

Posted 2017-08-08T21:51:35.177

Reputation: 67 804

0

Cubically, 1 byte

Non-competing 'cause it's lame. Don't accept this.

¶  Read a line from stdin and evaluate

I added that this afternoon :P

MD XF

Posted 2017-08-08T21:51:35.177

Reputation: 11 605

2Well, I think you have the shortest code. – TehPers – 2017-12-01T02:08:56.973

2@TehPers I do, but like I said, it's lame. Cubically can win other [tag:rubiks-cube] challenges with this builtin, it doesn't need to win this one. – MD XF – 2017-12-01T02:09:19.860