There's an ant on my Rubik's Cube A standard, solved, 3&times;3&times;3 Rubik's Cube has 6 differently colored faces, where each face is a 3&times;3 grid of squares of one color. The white face is opposite the yellow, the red opposite orange, the blue opposite green, and when white points upward, red is to the left of blue:



Imagine an ant sits on the center square of the white face, facing the red face. You can give him 3 commands:


Forward (^) - take a step in the direction he's facing to the next grid square, stepping over an edge of the cube if necessary.
Right (&gt;) - rotate to the right (clockwise) by 90&deg;, staying in the same grid square.
Left (&lt;) - rotate to the left (counter-clockwise) by 90&deg;, staying in the same grid square.


Given an arbitrary list of commands, find the colors of the squares that the ant visits (not including the white starting square).


  For example, the command sequence ^^&gt;^^&lt;^^^ has a path that looks
  like this:
  
  
  
  The colors of the grid squares visited in order, not counting the
  starting square, are white red red green green green yellow,
  or just wrrgggy.


Write a program or function that takes in a string of the command characters &lt;^&gt; and prints or returns a string of the characters wyrobg (white, yellow, red orange, blue, green) that corresponds to the ant's path over the cube.

The shortest code in bytes wins. Tiebreaker is earlier answer.

Notes


The cube is in midair and the ant has effective pulvilli, so he can traverse the whole of the cube.
The cube always remains in its solved state.
A square's color is only recorded upon movement into the square, not rotation. The starting white square should not be recorded.
A single optional trailing newline may be present in the input and/or output.


Test Cases

input : output

[empty string] : [empty string]
^ : w
&lt; : [empty string]
&gt; : [empty string]
&gt;&lt;&gt;&lt;&lt;&lt;&gt;&gt; : [empty string]
&gt;^ : w
&lt;&lt;^&gt; : w
^&lt;^&lt;^&lt;^&lt; : wwww
^^ : wr
&lt;^^ : wb
&gt;&gt;&lt;&lt;&lt;&lt;^^ : wo
&lt;^^^&lt;^^^^&lt;&gt;^&gt;^^&gt;^ : wbbboooggyo
^^^^^^^^^^^^^^ : wrrryyyooowwwr
&lt;&lt;&lt;^&lt;^&gt;^&lt;^&lt;^&lt;^&gt;^^^^&lt;^&gt;&gt;&gt;&gt;&gt;^^&lt;^&gt;^^&lt;^&gt;^&gt;^&gt;^&gt;&lt; : wwgrwgggoooobbbbyrby
^^&gt;^^&lt;^^^ : wrrgggy
Perl, 156 143 134 128 127 125 120 119 117 113 109 bytes

Includes +1 for -p

Run with the control string on STDIN, e.g.

perl -p rubic.pl &lt;&lt;&lt; "^^&gt;^^&lt;^^^"


rubic.pl:

@1=wryobg=~/./g;s##$n=w&amp;$&amp;;$y+=$x-=$y+=$x,@1[0,4,2,5,3,1]=@1while--$n%9;@{$n&amp;&amp;--$y%3}[3,0..2]=@1;$1[$n+9]#eg


Explanation

Older version:

@f=gboyrw=~/./g;s##$n=w&amp;$&amp;;$y+=$x-=$y+=$x,@f=@f[2,4,1,3,0,5]while--$n%9;@f=@f[0,$y=1,5,2..4]if$n&amp;&amp;$y--&lt;0;$f[$n+8]#eg


The challenge of this question is to find a coordinate system that makes it easy to track the position and direction of the ant and to still easily get the face identity. 

The system I chose was to put standard (x,y) coordinates on the face the ant is on such that the ant is always facing in the negative y direction with the center of the face being (0,0). So:

rotate right: (x',y') &lt;- (-y,  x)
rotate left:  (x',y') &lt;- ( y, -x)  alternatve: 3 right rotations
Step forward:   y' &lt;- y-1


If y was already -1 the ant will leave the current face and step onto the next one. In the new coordinate system there x keeps its value, but y' becomes 1.

This gives an easy coordinate system within a face. I also need something for the faces themselves. There I use an array consisting of

The face to right of the ant            g in the initial position
The face to the left of of the ant      b
The face behind the ant                 o
The face opposite to the ant            y
The face before the ant                 r
The face the ant is on                  w


So the initial array is (g,b,o,y,r,w). Moving to the next face corresponds to rotating the last 4 elements, so moving from white to red makes this (g,b,w,o,y,r). Turning right is a permutation of the first 5 elements giving (o,r,b,y,g,w). Turning left is a simular permutation but can also be done by turning right 3 times, so applying this permutation 3 times. And not turning at all can also be done by applying the permutation 8 times. In fact turning right can also be done by applying the permutation 5 times.

Knowing this the program is rather simple:

@f=gboyrw=~/./g                 Set up the initial face orientation
s## ... #eg                     Process each control string character
                                {this is equivalent to s#.#...#eg because
                                the empty regex repeats the last
                                succesful regex)
$n=w&amp;$&amp;                         Calculate n, the number of right
                                rotations+1 modulo 9.
                                This abuses a coincidence of the control
                                characters:
                                 "&lt;" &amp; "w" = "4" -&gt; 3 right rotations
                                 "&gt;" &amp; "w" = "6" -&gt; 5 right rotations
                                 "^" &amp; "w" = "V" = 0 but that is 9 mod 9
                                 so leads to 8 right rtations

$y+=$x-=$y+=$x,                 This is the same as ($x,$y)=(-$y,$x), so
                                a right rotation of the face coordinates
@f=@f[2,4,1,3,0,5]              Right rotation of the face array
   while --$n%9                 Rotate right n-1 times. After this n=0
                                If this was a step then n was effectively 0.
                                So rotate right 8 times leaving n=-9

    ... if $n                   If a step...
               $y--             ... decrease y ...
             &amp;&amp;$y--&lt;0           ... but if y was already negative ...
@f=@f[0,$y=1,5,2..4]            ... change face and set y to 1

$f[$n+8]                        return the last element (current face)
                                if this was a step, otherwise empty


So for that last statement rotations lead to the empty string and steps forward lead to the current face. Therefore $_ gets replaced by the faces visited on each step.