[Question]
  [
You place a standard die at the origin of a 2D grid that stretches infinitely in every direction. You place the die such that the 1 is facing upwards, the 2 is facing in the negative y direction, and the 3 is facing in the positive x direction, as shown in the figure below:
![](https://i.stack.imgur.com/iMrcp.png)
You then proceed to execute a series of moves with the die by rotating it 90 degrees in the direction of motion. For example, if you were to first rotate the die in the negative x direction, a 3 would be upwards, the 2 would be facing in the negative y direction, and a 6 would be facing in the positive x direction.
The series of moves `+y, +y, +x, +x, -y` is shown in the figure below, along with the net of the die for clarification (sometimes the net is called a 'right-handed die').
![](https://i.stack.imgur.com/FeOzU.png)
We then proceed to read off the top face of the die after every move. In this case it would read `2, 6, 4, 1, 2`, which we call a *dice path*. Note we do not include the top face of the die in its initial position, but it is always 1.
If the path of the die is such that it returns to the square it started on at the end of its movement, we call this a *dice path that returns to the origin*.
## Challenge
Given a nonempty dice path as input (in a list or any other reasonable format), print a truthy value if the dice path returns to the origin, and a falsy value otherwise. Note that:
* The truthy values and falsy values you output do not have to be consistent, but you can't swap them (eg. output a falsy value for a path that returns to the origin and a truthy value otherwise)
* The input will be well formed and represent a valid dice path.
* There is no limit to how far the die can stray from the origin.
## Test Cases
```
Path                            -> Output
2,1                             -> true
3,1                             -> true
5,4,1,5                         -> true
2,4,1,2                         -> true
4,2,4,1                         -> true
2,4,6,2,4,6,5,4                 -> true
2,4,5,1,4,5,3,6,5,1             -> true
5,6,2,3,5,4,6,3,1,5,6,2         -> true
2,4,1,3,5,1,3,5,6,3,5,6,4,5,6,2 -> true
2                               -> false
4,5                             -> false
5,1,2                           -> false
5,6,2,1                         -> false
5,4,6,5,4,6                     -> false
5,6,4,1,5,4,2,6,5,4             -> false
5,1,2,1,5,6,5,1,2,6,4           -> false
4,6,3,1,5,6,2,1,3,6,4,1         -> false
```
## Scoring
Shortest code in bytes wins.
  
[Answer]
# 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!](https://tio.run/##lZLBbsIwDIbvewp2cjNCaZo4nNIT2nWX3bJsimgLnbq0gjI2ae/ehQEaIFBpZEVJ9H@x7N/v9tOuZsuibkauSrM2V61ViQ0/bB04lUzV9O8YvNGC@KsGjISIMJrISE5Apw9yWCQJM6@FofVQ6Y2yYZm5ebOgow1ldMSMTv0nhUuzr6c8cMQQWquITpUOwxBiFJxJMOTnvm5nlVtVZRaW1TwA/bxcN4tvA@Tu@D0PdhyDwckyhAzG40GzXGeX9LynHgVD6KGPBYv76IUH@v0vfaCAm/XIBHKJhyxd9cqYo5Cc@QPcVi9HH9KH2DLH@hMAXpx@tOXqqpNnxhwy55653DqEfgCeWdMNyPiiOdeBrTUS@mTw8yXif0NvqMFb43e5Jzq7tPOScbkftGOg/QU "JavaScript (Node.js) – 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 orientation of the die as described in the challenge, using this 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!](https://tio.run/##dY/LDoIwEEX3fMVdmBRESFHABcqPEAwN9VFiqBF1o3w7XjQmurDJySzmTO9Mo26qq8/mdAlaq7fDUNu2s8dteLR7VwAwWOMBSSIyJwsSk0R4mfOjB@/n/2H0nZ09u5p/ygwaK8Qsvu/h7gBjS7FVlLNXLBVDJWX5KIAKT9fu4IpExrFM5DKV6VIUGlOk8OnnOaISGxiGAT35XrHCmD25654HVBxQYWNNy0sfEB5H@mF4Ag "JavaScript (Node.js) – Try It Online")
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.
[Answer]
# [J](http://jsoftware.com/), 79 bytes
Takes in the dice path as a reversed list of boxed 0 to 5.
```
0 0-:[:+/(0,(,-@|.)=i.2){~]i.~&>[:}.(]A.~0 224 283 389 489{~i.~)&.>/\.@,&(<i.6)
```
[Try it online!](https://tio.run/##ldHNbsIwDAfwO09h7VAa0RrXSVFJADFN2mmnXRknBKJc9gBsffUudboPpBFKpbpS9ZPj/H1qH3B8gKWFMWRAYP2bIzy9vjy3BJTbjZ1MU8rSLF9/oFrWyOrcbGtsktXGfmK6fcSGgNkAVxp0NQdTzc@NByrB1fQN11mSLmqcqVaN9rvjOxSQWzhA7CFXwB2Yh2HjyGlnhuFC8MAxhDodx/5oVwos451JIPuqHUm9grtO3aVYOhr/lT@XOCAtlftKff3tTLdzhsUd2PykcRN3SZthmC4uGMEhj/LPuv/BgRRCKSR0DWuBYTEdjswcOrHrZ/1eD4zaLw "J – Try It Online")
### How it works
We have the starting dice as the list `0 1 2 3 4 5`. Looking for the next top digit, we have either `1 2 3 4` as its index (`0` and `5` would be illegal moves). Taking the anagram indices `224 283 389 489` we permute the list, executing a dice move. We do this for the whole path and gather the intermediate results: `0 1 2 3 4 5│4 0 2 3 5 1│3 0 4 1 5 2`. Again, after looking for the indices, we map them to coordination changes `_1 0, 0 _1, 1 0, 0 1`, sum them up and check if they end up at `0 0`.
There should be a byte saving by remapping the dice numbers 6 to 4, 5 to 3, …, 1 to 5 to save the two dummy zeros (`0,` and `0` ) by shifting the possible indices to `0 1 2 3`. But this feels so wrong that I'll try to think of another solution first. :-)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~55~~ 48 bytes
```
≔324516θＦＳ«≔⌕θιιＭ✳⊗ι≔⭆§⪪”)⊟？2p}Þ↔～>”⁶ι§θＩκθ»¬∨ⅈⅉ
```
[Try it online!](https://tio.run/##NY7NC4JAEMXP@leIp1kocL@8eJIi8NAHdKmjqdWS7Oq6RRD97duIBm8uM/N@71X30lambL3Ph0HdNMScCUnTeBH1JAuvxkZQ6O7pjs4qfQNCok8YzL8bpWvoF5Ei42RhsDWvBtbKNpVTRsPaPC9tU4MiZLzOrom0LTvIXaHr5g3HrlUOYkkFT1gimaBcUMlZkggmOcUuKZlS/g4MXZWDgweSp6bf8IBYBzvjYG/hBLg/Y11M9n4kSlSKEjJlfvlqfw "Charcoal – Try It Online") Link is to verbose version of code. Takes input as a string of digits and outputs a Charcoal boolean, i.e. `-` for back at the origin, whitespace if not (+2 bytes to remove the whitespace). Explanation:
```
≔324516θ
```
The digits on the die, in the order right, up, left, down, top, bottom.
```
ＦＳ«
```
Loop through the input digits.
```
≔⌕θιι
```
Find in which direction the die was rolled.
```
Ｍ✳⊗ι
```
Move in that direction.
```
≔⭆§⪪”)⊟？2p}Þ↔～>”⁶ι§θＩκθ»¬∨ⅈⅉ
```
Permute the digits to their new positions using a lookup table `514302` `052413` `415320` `042531` depending on the direction.
```
»¬∨ⅈⅉ
```
Did we end up back at the origin?
[Answer]
# [Python 2](https://docs.python.org/2/), 91 bytes
```
a=b=1
t=0
for c in input():t=1+t*1j**(a*c*(a*a-c*c)*b**5%7*2/3+2*(a==c));a,b=b,c
print t==0
```
[Try it online!](https://tio.run/##bU9hC4IwFPzurxhCoM9FbXMGxvsl0QcdRkaYxKL69TZ72yoIRO/e@e7ujU97vAxyuh/7c8dE3T06k6bp1GCLIrG4Tg6XKzOsH9wz3myW1xZFYUGcALIGzPxqlgZMDi2AXmxArlQh3RjR5Pm24S223CTjtR8ss4jrafbfSc7EPtkp@mjOSoc4047IQKQjDnkelSqMHJgXo6DfWwRUkL09LSnaeFNFgSR9x6rgpIIcQfmzQPU0Bfi@MepzVhVDo16GbDrvc4g3@qoWJxX98ad8KOuN9y8 "Python 2 – Try It Online")
The idea is to translate each triplet of consecutive die faces shown into the corresponding turn made by the die's path. The possible turn directions are left, right, straight, or reversing, all taken relative to the die's previous move. From the sequence of turns, we track the die's current coordinate and check whether it returns to the origin. Instead of tracking the die's facing direction, we simply rotate the whole coordinate system around it when it turns, and then move it.
Doing it this way means we don't have to track the state of the die itself -- just looking at local snippets of the input sequence suffices. We also don't use any hardcoded values or magic numbers.
The tricky bit is extracting the turn direction from the three consecutive die faces. You can think of these as a bug crawling on the from the first face to the second face, and then from the second face to the third face -- which direction does it need to turn on the second face to do this? We can detect that it doubles back if the first and third faces are equal, and that it goes straight forward when the first and third faces are opposite, so they add to 7.
In the remaining cases, it remains to detect whether the bug turns left of right, that is the triple of faces is left-handed or right-handed. For this, we borrow an algebraic trick from [my CW solution to Determine dice value from side view](https://codegolf.stackexchange.com/a/44370/20260). The expression `3*a*c*(a*a-c*c)` will equal either `b` or `-b` modulo 7 depending on whether the triple is right-handed or left-handed. From there, some massaging takes the four cases of turn directions to numbers that equal `0,1,2,3` modulo 4, so that we can get the right complex rotation using the complex exponent `1j**`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 35 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
6RW;⁸œ?@\ḊḢ€⁼
“§ṫ²G‘×J$ṗLçƇ:19Ḣı*S¬
```
A monadic Link accepting the dice-path as a list of the faces in \$[1,6]\$ which yields `1` (truthy) if it ends back at the start, or `0` (falsey) otherwise.
**[Try it online!](https://tio.run/##AVAAr/9qZWxsef//NlJXO@KBuMWTP0Bc4biK4bii4oKs4oG8CuKAnMKn4bmrwrJH4oCYw5dKJOG5l0zDp8aHOjE54biixLEqU8Ks////NCwyLDQsMQ "Jelly – Try It Online")** Or see the [test-suite](https://tio.run/##y0rNyan8/98sKNz6UeOOo5PtHWIe7uh6uGPRo6Y1jxr3cD1qmHNo@cOdqw9tcn/UMOPwdC@Vhzun@xxefqzdytASqOzIRq3gQ2v@H24Hqv//PzraSMcwlksn2hhCmeqY6BjqmIKYRmCmEYhpogPmwETNdCAkUDFYCKLEFKIfqsUUrApuJlixjlksVywA "Jelly – Try It Online") (6 longest removed since the method is inefficient).
### How?
Forms all the possible paths of the given dice-path's length formed from the four possible directions as the permutations-indices of the next state.  
Filters these to find the one that matches the face-up numbers in the given dice-path.  
Translates the permutation indices to the four Cartesian directions as complex numbers.  
Checks if the sum of these is zero.
```
“§ṫ²G‘×J$ṗLçƇ:19Ḣı*S¬ - Main Link: dice-path
“§ṫ²G‘                - list of code-page indices = [225,245,130,71]
      ×J$             - multiply by their indices = [225,490,390,284]
                        (these correspond to [up, down, right, left])
          L           - length (of the dice-path)
         ṗ            - Cartesian power (all lists of that length using {225,490,390,284})
            Ƈ         - filter keep those for which:
           ç          -   call Link 1 as a dyad - f(potential-path, dice-path)
             :19      - integer divide by 19 (225,490,390,284 -> 11,25,20,14)
                Ḣ     - head (get the single path that filering found)
                        (having Ḣ here rather than before the :19 saves a byte)
                 ı*   - i exponentiate (that) (11,25,20,14 -> -i,i,1,-1)
                        (yep we've mirrored but it makes no difference)
                   S  - sum
                    ¬ - logical NOT
6RW;⁸œ?@\ḊḢ€⁼ - Link 1: potential-path (as permutation indices), dice-path
6             - six
 R            - range -> [1,2,3,4,5,6]
  W           - wrap -> [[1,2,3,4,5,6]]
   ;⁸         - concatenate with the permutation indices -> [[1,2,3,4,5,6],a,b,c,...]
        \     - cumulative reduce (current-state, permuation index) by:
       @      -   with swapped arguments:
     œ?       -     permuation (of the current state) at index (permutation index)
         Ḋ    - dequeue (remove the leading [1,2,3,4,5,6])
          Ḣ€  - head of each (get the list of face-up pips)
            ⁼ - equals (the dice-path)?
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~96...74~~ 72 bytes
```
<<Quaternions`
0==Tr[a={-1,-K,J,-J,K,1};#&[t=a[[#]],a=++t**a**t/2]&/@#]&
```
[Try it online!](https://tio.run/##VU4xDoJAEOz5BgkFLlE4oNEz1thotLuQuDEaKcAEz4rg1/F2FzA2e7NzM7NTo33carTVFYdhszm@0d7apno2r4t31yutz61B3UUxRHsoICpgD3G/9gNjNRrjlyWgXixsGGIY2mVSBsudXwbDoa0aa/xoe6f1c7pi8@m8LnFm8DolTwYpxJARTBgmBFPgZWJzkOnEE5U5KU3F9BhFOkUqhxTFEvOLVuxSTMtMZ4nTyOFMosYikjk3zSV8@uHqQGXnbuwcTwvO5eOvE9dgf@/1wxc "Wolfram Language (Mathematica) – Try It Online")
`Quaternions`` must be loaded prior to the function definition.
```
<<Quaternions`              (* load the Quaternions` package *)
a={-1,-K,J,-J,K,1};         (* (a die roll of i moves the die in direction a[[i]]) *)
            a=++t**a**t/2   (* rotate `a` in a direction *)
#&[t=a[[#]],             ]& (* and return the direction, *)
% /@#                       (* taking directions one at a time from input. *)
0==Tr[ % ]&                 (* check if the sum of directions returns to the origin. *)
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~174~~ \$\cdots\$ ~~130~~ 129 bytes
Saved a whopping ~~15~~ ~~30~~ 35 bytes thanks to the man himself [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!!  
Saved another whopping ~~3~~ ~~9~~ 10 bytes thanks to [ovs](https://codegolf.stackexchange.com/users/64121/ovs)!!!
```
def f(l,p=[3,2,4,5,1,6],v=0):
 for d in l:n=p.index(d);v+=1j**n;p=[p[int(i)]for i in'%06d'%ord('񽣾첽񥙘꘣'[n])]
 return v==0
```
[Try it online!](https://tio.run/##dVJBTsMwELznFXupbJcF1XESpFbmyAu4hRwQSURQ5UaOW4Gq/qOcKz7AvTwIcekLQuykErQmh9V6PLvemWz9ap4WSrRtXpRQ0jnWMhUYYoQxckwyXMkJmwZQLjTkUCmYT5WsryqVFy80Z7PVheTP47GadXV1WilDK5ZZctWRyWiS5GS00Dklh/3u8/tjf3h/235tdyRVGcsC0IVZagUrKSetKRrTgAQaQPdRGiJnCHd6WTAcIHEOxd2oHONTOHRweApHThr3sRPsY9fQd23tsFE4imcMWy9sdZcJO5JF/GMJ1004Sh@j/@gdcvswb34riM@weJB6itqRuAcfZGLirXF@onXqaIbntUFgnyce1h8bnNpkcP7IY4G2f5vcL8Pr6JEguIwLwgK7QAYLu29uK6auqV2OkhrmDrW2y1aStdnA5Q2smw2sddpIWWQbwtof "Python 3 – Try It Online")
Returns `True` if we end up back at the origin or `False` otherwise.
Port of [Neil](https://codegolf.stackexchange.com/users/17602/neil)'s [Charcoal answer](https://codegolf.stackexchange.com/a/211541/9481) using complex arithmetic to find out if we're back where we started.
[Answer]
# [R](https://www.r-project.org/), ~~143 ... 116~~ 102 bytes
*Edits: -13 bytes by switching to linear instead of matrix list of transitions, then -4 bytes by halving the list of transitions & calculating the left, back & bottom die values as 7 minus the right, front & top values at each roll, then -3 bytes by switching to a base-7-encoded number to generate the list of transitions, then -14 bytes by rearranging the transition list to up, right, down, left to make calculating the new position easier using powers of i, and -6 bytes by various other minor golfs that didn't alter the approach*
```
p=a=1:4;m=5032105982%/%7^(11:0)%%7;for(i in scan()){p[6:4]=7-p;p=p[m[q<-(p[m[a]]==i)]];F=F+1i^a[q]};!F
```
[Try it online!](https://tio.run/##LcWxCsIwEADQ3a84h0CCFHNp2mjirfmJcIUgCBla0zqK3x5Rury3tVYpE3obZhp0b1AP14sRZ@Emiei1EsKFx3OTBcoCr3tepFLvmkZvmVxXQ6Wa5rTeOvk7MxMVxRwixROWKaeVP@EYmwELCD0Mu@Ou/WsO7Qs "R – Try It Online")
**How?**
(commented code before golfing)
```
is_dice_loop=
function(s,                     # s = vector of top die values along path
 p=1:6,                         # p = positions of current die values
                                #     (top,front,right,left,back,bot)
 m=matrix(                      # m = matrix of transitions at each roll
  utf8ToInt(                    #     created from ASCII values of
   "bedcfabbccafddfaafeeebcd")  #     this string
   -96,                         #     -96,
  4)                            #     matrix has 4 rows.
 ){
 for(i in s){                   # Now, for each die value i along the path
  r=match(i,p[m[,1]]);          #  calculate the roll direction r
                                #  (1:4 -> up,down,right,left),
  p=p[m[r,]];                   #  then calculate the new positions of die values,
  F=F+(-.5+r%%2)*1i^(r>2)       #  and calculate the new location of the die
                                #  as a complex number (real=left->right, imaginary=down->up)
                                #  (F is initialized as 0+0i by default).
 }
 !F                             # If we end up back at 0+0i, then we've done a loop,
}                               # so NOT F is true.  
```
[Answer]
# [Perl 5](https://www.perl.org/) `-pF`, ~~130~~, 121 bytes
```
@A=(2,4,-1,1,-@F,@F+!s/^/1/);s/.(?=.(.))/$x+=$A[$&==$1||$&+$1!=7&&($P[$|--]=$&)&&$1==$P[$|]?$A[$|]^=1:$A[$|]]/ge;$_=$x==1
```
[Try it online!](https://tio.run/##LYxBa8MwDIXv/hcFYRLixJMtubAgml5y7r2kO5Ux6NbQ7JCBf/s8ZRs8xCfpvTdfHzcu718VrG441T1keerLcJQqOHItOnTtMLphbHaLv3j0db/4rjpIV3V17WFtBI5nsCKAOYNtAHeyt7aC0xly204CtrYWUA3bZTps9jxdBJ//aPKv1x5eBFYRLCWgiWiYkE0gDIZ0KiUVkwIjcUyslhQiU4qosFkjq5KKfg@G2LDmddnqNJyUtZbCVqQvDepMZP5LMOr7@z5/vt0/ltLe5vEH "Perl 5 – Try It Online")
A different solution, the straightforward one was [173 bytes](https://tio.run/##RY3BCsIwEETv@Qshh0pTdZPdHCzFHkp/ohRPCoLaUj3UnzdOY1BYwuzOm8l4mq4Sbi89l6Fuqow2G78ut7rpqN8esrrpdoaMGO4r6KjMzug5z/v1fsE4YcmKWIoAK4qE2V@bheUiFlVsK6q6BZj9j6nnu@G7CJyHqW5LfaxWeg7BknKkhEmUZbKK8UJ5jDCEEIvzAsRbJ@wdQSyoE4zHcDwoFiXIY1nqEPbQqGW7FMFCEK9nlUrIwX4P4/My3B@huI7tBw "Perl 5 – Try It Online"), which could be golfed to [165 bytes](https://tio.run/##LU7RasMwEHv3XwxMm9Cm7tl3flgwzYPJTyxhDNqNQdaEdozs5@fJ3uCQddJJeLncJkkf33ptUxdDRYeDr1v9HLYm0mhOVTyS8Bgi8LjX62431o8mcrGyBitfwGqaYtm/lBUHC5hTTeh6mFXZcwJvLivy63zr@m17N9EMXXwyb6DDuTqF4VwbvdlDuHy9TPlTD3pNyZJypIRJlGWyioFgHiMMIsTivODEWyfsHYHkUycYj@EiKBYlyGPJdQh7cNSyzUWwEAR6Vv8l5GD/zMvn@3y9p2Za@l8 "Perl 5 – Try It Online").
EDIT : I realized after that the straightforward could be golfed to [136 bytes](https://tio.run/##LYzBCsJADETv@xdCD4pbNZtkDy3FHkp/QoonBaHaUj3Un3edWCFkZ@dlZrxMvab7O5vLVDfVmna7uCmh9nVzom5/tMerD579wYvP5u22KwzKAgW2QfUEmOcLDAsM3rKWY4NV3XYFbIZ9ABAkUWj2dZjqtszO1SqbUwrkmJwKqQtCwQk2VMSoQCiJclScxMAqkQnCTlkxESM/w4k6RR4fq0M4QqNWghUBIYgdxf1LiIE/w/i6DY9nyvux/QI)
But to golf more I though differently. Using the fact that the sum of opposite side is 7. And that keeping a track of some previous number could be sufficent to get the directions.
* array A :
	+ `[0]`={2|3} and `[1]`={4|5} : to store the direction on (+/-)x or (+/-)y, where x and y depend on the firsts move's direction on these axes
	+ the next `[2..5]` : to store numbers to add to `$x` corresponding to direction
* `s/^/1/` : prepend `1` the initial face
* regex `.(?=.(.))` : consumes one die face `$&` and capture the next next `$1`
* `$&==$1` the direction is changing backward
* `$&+$1==7` the direction doesn't change, the test is inverted because nothing to do
* otherwise the direction has changed to rigth or left compared to the previous direction
* array P : stores the last die face [0] x, [1] y, where x and y are depends on the firsts moves
	+ `$P[$|--]=$&` :
		- `$P[$|]=$&` to store the die face when turning left or right
		- `$|--` switches axis index 0/1 for x/y
* `$1==$P[$|]` the next next face is compared to the last when moving on the same axis if is equal the direction is changing backward compared to the previous direction
* `^=1` : to switch using bytwise xor (2<->3) or (4<->5)
* `$_=$x==1` : the initial position when `$x==1` because first move (direction 2 : -1) was not added.
]