# [Ruby], <s>126 124 </s>122 bytes Saved 2 bytes by changing output colours from `ABCEFG` to `123456` and another 2 by deleting the `[]` from around `[1,2,3]` <!-- language-all: lang-ruby --> ->n{a=($/+?.*w=n.size*4)*w a[q=w*w/2]=?2 c=1,2,3 n.map{|i|c[j=i/2,2]=c[j+1]*s= ~0**i,-c[j]*s a[q+=s+j*w*s]="#{c[1]%7}"} a} [Try it online!][TIO-le1vqw24] [Ruby]: https://www.ruby-lang.org/ [TIO-le1vqw24]: https://tio.run/##TYzdCoJAFITv9ynECurs@rcGXZ18kOVcbJKwQmJtspTaq28aQjIMzHwD8@guL1@hj85Nr3G/TXgRg8MmtuZ9heMBHNPqjg5cIgkLyUrMhBQ5a@KbbvvBDKWq0SRSTPMUeUZgMfikAEZEE5jq/MDR8hocWMJw05cqo91pDEemR992TxtUSqViERH7Myl@XrGZ5Kuei0VE/gs "Ruby – Try It Online" Takes an array of numbers `0,1,2,3` corresponding to `E,W,S,N` or equivalently `x+=1,x-=1, z+=1,z-=1`. Outputs the path of the cube traced on a string of `w*w` periods, where `w` is four times the length of the input. The cube is represented as a vector `c=[x,y,z]` starting at `[1,2,3]` which rolls in the `xz` plane. The colour is determined by the current `y` coordinate. If the cube is rotated 180 degrees, the y coordinate becomes negative, so the possible values are `-3,-2,-1,1,2,3` with opposite faces adding to zero. To display, the number is taken mod 7 and this is converted to a character, so the possible displayed colours are `1,2,3,4,5,6` with opposite faces adding to 7. The vector is rotated by 90 degrees by swapping two coordinates and changing the sign of one of them, similar to Arnauld's answer, but where he uses an array, I use the formula `c[j,2]=c[j+1]*s,-c[j]*s` where `c[j,2]` is a block of 2 array elements starting at `j`=0 or 1 and `s` is the sign of the rotation, +1 or -1. `s` is also the sign of the direction of movement and is calculated as `(-1)**i`. The brackets are necessary since unary minus has lower priority than power in Ruby. To avoid brackets, we represent `-1` as `~0` because the bit-inversion operator has higher priority than raising to a power, but we still need to leave a space to avoid `=~` being interpreted as a match operator. **Commented code** ->n{a=($/+?.*w=n.size*4)*w #make a string of w*w periods separated in lines by /n a[q=w*w/2]=?2 #set q to the centre of the field (padding by the /n characters ensures w*w/2 is central horizontally as well as vertically) c=1,2,3 #put a 2 at position q. Set up a vector c for colours x,y,z n.map{|i| #iterate through array of directions. j=1 for vertical, 0 for horizontal. s=1 for increase, -1 for decrease c[j=i/2,2]=c[j+1]*s= ~0**i,-c[j]*s #in accordance with value of j, swap c[0]&c[1] or c[1]&c[2] and change the sign of one in accordance with s a[q+=s+j*w*s]="#{c[1]%7}"} #modify the value of q by s (horizontal) or (1+w)s (vertical) and save colour%7 at a[q] a} #return the final string a. # [Ruby], 131 bytes Version with 2D array is slightly longer due to clumsy 2D array initialization in Ruby. <!-- language-all: lang-ruby --> ->n{a=(0..w=n.size*4).map{[0]*w} c=[1,a[z=x=w/2][x]=2,3] n.map{|i|c[j=i/2,2]=c[j+1]*s= ~0**i,-c[j]*s a[z+=s*j][x+=s-s*j]=c[1]%7} a} [Try it online!][TIO-le0qhmga] [Ruby]: https://www.ruby-lang.org/ [TIO-le0qhmga]: https://tio.run/##dYzBCoMwDIbvfQovg63WWutgp@xFSg6dTKgwkXVSp3av3lVxhx3kJ@RPvvx59rd3qCFk13bScBScO2i5NeOdnk/8obtJCaTOkwpUwbQaYQCXS1QDgmQlknY9ms1cqQZMLplEiDYtkFpIPoJSw7K4iCOJ8RQsbWI69mxx8bbAw8UT7UOtlGCbEH9/u8R40vUvS1Yu2Vo7fKHlDivZpn8evg "Ruby – Try It Online"