Ruby, 126 124 122 122119 bytes
Saved 2 bytes saved by changing output colours from ABCEFG
to 123456
and another, 2 bytes saved by deleting the []
from around [1,2,3]
, 3 bytes saved by changing directions from 0,1,2,3
to -2,-1,0,1
.
->n{a=($/+?.*w=n.size*4)*w
a[q=w*w/2]=?2
c=1,2,3
n.map{|i|c[j=i/2|i|c[j=i&1,2]=c[j+1]*s= ~0**i2]=c[j+1]*s=i|1,-c[j]*s
a[q+=s+j*w*s]="#{c[1]%7}"}
a}
Takes an array of numbers 0-2,-1,20,31
corresponding to E,W,S,N,E,S
or equivalently x+=1,x-=1, z+=1,z-=1, x+=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.
s
is also the sign of the direction of movement and is calculated as (i|1
. s
equals -1 for -1)**i2|1
and -1|1
, and 1 for 0|1
and 1|1
. The brackets are necessary since unary minus has lower priority than powerj
is calculated as i&1
and equals 0 for even numbers -2
and 0
(travel in Ruby. To avoid brackets, we representx
direction) and 1 for odd numbers -1
asand ~01
because the bit-inversion operator has higher priority than raising to a power, but we still need to leave a space to avoid (travel in =~z
being interpreted as a match operatordirection.)
->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/2c[j=i&1,2]=c[j+1]*s= ~0**i2]=c[j+1]*s=i|1,-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.
->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}