JavaScript (ES6), 142 ... 122 121 bytes
Expects an array of characters, e.g. ['5','1','2']
. Returns 0 or 1.
a=>a.map(n=>D=D.map((_,i)=>D['504405076067'[d*6+i>>1]^i],p+=[w=a.length,-w,1,-1][d=D.indexOf(n)]),p=0,D=[...'254316'])|!p
How?
The array D[]
holds the face values in the following order:
index | 0 | 1 | 2 | 3 | 4 | 5
-------+--------+--------+--------+--------+--------+--------
face | front | behind | left | right | top | bottom
We start with D = ['2','5','4','3','1','6']
, which is the initial position of the die described in the challenge, according to our encoding.
The direction d
of the next move is the 0-indexed position of the new top face in D[]
:
index (d) | 0 | 1 | 2 | 3
-----------+-------+-------+-------+-------
direction | up | down | right | left
When moving towards direction d
, the i
-th face in the updated die is the face at the following position in the previous die:
i = | 0 | 1 | 2 | 3 | 4 | 5
-------+---+---+---+---+---+---
d = 0 | 5 | 4 | 2 | 3 | 0 | 1
d = 1 | 4 | 5 | 2 | 3 | 1 | 0
d = 2 | 0 | 1 | 5 | 4 | 2 | 3
d = 3 | 0 | 1 | 4 | 5 | 3 | 2
This table is encoded with the following expression:
'504405076067'[d * 6 + i >> 1] ^ i
At each iteration, we update D[]
and the position p
. We test whether we're back to our starting point at the end of the process.