8 fixed a misleading comment in the source
source | link
a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[k]b[b]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1 (or 1 if b[b] is undefined)
    b = [...(…)]        //   initialize b[] (see above)
  ) && r > 2                 // end of map();
  && r > 2              // return true if rthe last result is greater than 2
a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[k]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1
    b = [...(…)]        //   initialize b[] (see above)
  ) && r > 2            // end of map(); return true if r is greater than 2
a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[b]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1 (or 1 if b[b] is undefined)
    b = [...(…)]        //   initialize b[] (see above)
  )                     // end of map()
  && r > 2              // return true if the last result is greater than 2
7 minor update
source | link

The values used to identify the pieces do not really matter as long as identical pieces have identical valuesthere's one unique value per piece type.

  • '89ABCA981111111' → the 8 black major pieces, followed by the first 7 black pawns
  • 10n**32n → the last black pawn on \$\text{h7}\$ (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)
    a b c d e f g h
  +----------------
8 | 8 9 A B C A 9 8
7 | 1 1 1 1 1 1 1 1
6 | 0 0 0 0 0 0 0 0
5 | 0 0 0 0 0 0 0 0
4 | 0 0 0 0 0 0 0 0
3 | 0 0 0 0 0 0 0 0
2 | 2 2 2 2 2 2 2 2
1 | 3 4 5 6 7 5 4 3

The values used to identify the pieces do not matter as long as identical pieces have identical values.

  • '89ABCA981111111' → the 8 black major pieces, followed by the first 7 black pawns
  • 10n**32n → the last black pawn (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)
8 9 A B C A 9 8
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2
3 4 5 6 7 5 4 3

The values used to identify the pieces do not really matter as long as there's one unique value per piece type.

  • '89ABCA981111111' → the 8 black major pieces, followed by the first 7 black pawns
  • 10n**32n → the last black pawn on \$\text{h7}\$ (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)
    a b c d e f g h
  +----------------
8 | 8 9 A B C A 9 8
7 | 1 1 1 1 1 1 1 1
6 | 0 0 0 0 0 0 0 0
5 | 0 0 0 0 0 0 0 0
4 | 0 0 0 0 0 0 0 0
3 | 0 0 0 0 0 0 0 0
2 | 2 2 2 2 2 2 2 2
1 | 3 4 5 6 7 5 4 3
6 more detailed explanation
source | link

How?

How?Pieces

The values used to identify the pieces do not matter as long as identical pieces have identical values.

We use:

  • 0 for empty squares
  • 1 / 8 / 9 / A / B / C for ♟ / ♜ / ♞ / ♝ / ♛ / ♚
  • 2 / 3 / 4 / 5 / 6 / 7 for ♙ / ♖ / ♘ / ♗ / ♕ / ♔

Board and initial position

The board is stored in the array \$b\$ which is initialized by splitting the concatenation of the following parts:

  • '89ABCA981111111' → the 8 black major pieces, followed by the first 7 black pawns
  • 10n**32n → the last black pawn (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)

Keeping track of the positions

The variable \$b\$ is also used as an object to keep track of all encountered positions. EachThe key for each position is uniquely identified by using \$b\$ itself (but, but this time as an array), and implicitly coerced to a string.

Commented

This is why we do:

b[b] = -~b[b]

Commented

a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[k]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1
    b = [...(…)]        //   initialize b[] (see above)
  ) && r > 2            // end of main map(); return true if r is greater than 2

How?

The board is stored in the array \$b\$ which is initialized by splitting the concatenation of the following parts:

  • '89ABCA981111111' → the 8 black major pieces, followed the first 7 black pawns
  • 10n**32n → the last black pawn (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)

The variable \$b\$ is also used as an object to keep track of all encountered positions. Each position is uniquely identified by using \$b\$ itself (but this time as an array), implicitly coerced to a string.

Commented

a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[k]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1
    b = [...(…)]        //   initialize b[] (see above)
  ) && r > 2            // end of main map(); return true if r is greater than 2

How?

Pieces

The values used to identify the pieces do not matter as long as identical pieces have identical values.

We use:

  • 0 for empty squares
  • 1 / 8 / 9 / A / B / C for ♟ / ♜ / ♞ / ♝ / ♛ / ♚
  • 2 / 3 / 4 / 5 / 6 / 7 for ♙ / ♖ / ♘ / ♗ / ♕ / ♔

Board and initial position

The board is stored in the array \$b\$ which is initialized by splitting the concatenation of the following parts:

  • '89ABCA981111111' → the 8 black major pieces, followed by the first 7 black pawns
  • 10n**32n → the last black pawn (\$1\$) followed by 32 empty squares (\$0\$)
  • 0x7e5196ee74377 → all white pieces (expends to 2222222234567543 in decimal)

Keeping track of the positions

The variable \$b\$ is also used as an object to keep track of all encountered positions. The key for each position is \$b\$ itself, but this time as an array and implicitly coerced to a string.

This is why we do:

b[b] = -~b[b]

Commented

a =>                    // a[] = input
  [ a,                  // dummy entry to mark the initial position as encountered once
    ...a                // append the actual data
  ].map(([x, y]) =>     // for each pair of squares [x, y] in this array:
    r =                 //   store the last result in r
    b[                  //   update b[k]:
      b[                //     update b[x]:
        b[y] = b[x],    //       set b[y] to b[x]
        x               //       set b[x] ...
      ] = 0,            //     ... to 0
      b                 //     set b[b] ...
    ] = -~b[b],         //   ... to b[b] + 1
    b = [...(…)]        //   initialize b[] (see above)
  ) && r > 2            // end of map(); return true if r is greater than 2
5 saved 10 bytes
source | link
4 fixed version
source | link
    Post Undeleted by Arnauld
3 fixed version
source | link
    Post Deleted by Arnauld
2 added a commented version
source | link
1
source | link