Skip to main content
minor clarifications
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650

We start with D = ['2','5','4','3','1','6'], which is the initial positionorientation of the die as described in the challenge, according to ourusing this encoding.

At each iteration, we update D[] and the position p. We add \$\pm 1\$ to \$p\$ when moving horizontally, or \$\pm w\$ when moving vertically, where \$w\$ is the length of the input array (an upper bound of the distance that can be traveled in a single direction). We test whether we're back to our starting point at the end of the process.

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.

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.

We start with D = ['2','5','4','3','1','6'], which is the initial orientation of the die as described in the challenge, using this encoding.

At each iteration, we update D[] and the position p. We add \$\pm 1\$ to \$p\$ when moving horizontally, or \$\pm w\$ when moving vertically, where \$w\$ is the length of the input array (an upper bound of the distance that can be traveled in a single direction). We test whether we're back to our starting point at the end of the process.

added an explanation
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650

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

Try it online!

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

Try it online!

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.

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

Try it online!

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

Try it online!

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

Try it online!

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.

saved 1 byte
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650

JavaScript (ES6),  142 129 128... 122  122121 bytes

Expects an array of characters, e.g. ['5','1','2']. Returns 0 or 1.

a=>a.map(n=>D=[...D]n=>D=D.map((_,i)=>+D['504405076067'[d*6+i>>1]^i]=>D['504405076067'[d*6+i>>1]^i],p+=[w=a.length,-w,1,-1][d=D.indexOf(n)]),p=0,D='254316'D=[...'254316'])|!p

Try it online!Try it online!

JavaScript (ES6),  142 129 128  122 bytes

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

Try it online!

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

Try it online!

saved 6 bytes
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
Loading
saved 1 byte
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
Loading
saved 13 bytes
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
Loading
Source Link
Arnauld
  • 197.8k
  • 20
  • 180
  • 650
Loading