user
As described on the **Chess960** page, Chess960 (a.k.a Fischer Random Chess, Chess9LX) is a variant of chess where the array of pieces behind the pawns is randomized at the start of the game to minimize the value of opening theory "book knowledge". That task is to generate legal starting positions, and some of the solutions accept a standard Starting Position Identifier number ("SP-ID"), and generate the corresponding position.

Task

This task is to go the other way: given a starting array of pieces (provided in any form that suits your implementation, whether string or list or array, of letters or Unicode chess symbols or enum values, etc.), derive its unique SP-ID. For example, given the starting array **QNRBBNKR** (or **♕♘♖♗♗♘♔♖** or **♛♞♜♝♝♞♚♜**), which we assume is given as seen from White's side of the board from left to right, your (sub)program should return 105; given the starting lineup of standard chess, it should return 518.

You may assume the input is a valid Chess960 position; detecting invalid input (including illegal characters or starting arrays with the bishops on the same color square or the king not between the two rooks) is optional.

Algorithm

The derivation is the inverse of the algorithm given at Wikipedia, and goes like this (we'll use the standard chess setup as an example).

1. Ignoring the Queen and Bishops, find the positions of the Knights within the remaining five spaces (in the standard array they're in the second and fourth positions), and then find the index number of that combination. There's a table at the above Wikipedia article, but it's just the possible positions sorted left to right and numbered 0 to 9: 0=**NN---**, 1=**N-N--**, 2=**N--N-**, 3=**N---N**, 4=**-NN--**, etc; our pair is combination number 5. Call this number N. **N=5**

2. Still ignoring the Bishops, find the position of the Queen in the remaining 6 spaces; number them 0..5 from left to right and call the index of the Queen's position Q. In our example, **Q=2**.

3. Finally, find the positions of the two bishops within their respective sets of four like-colored squares. It's important to note here that the board in chess is placed such that the leftmost position on the home row is on a dark square and the rightmost a light. So if we number the squares of each color 0..3 from left to right, the dark bishop in the standard position is on square 1 (**D=1**), and the light bishop is on square 2 (**L=2**).

4. Then the position number is given by **4(4(6N + Q)+D)+L**, which reduces to **96N + 16Q + 4D + L**. In our example, that's 96×5 + 16×2 + 4×1 + 2 = 480 + 32 + 4 + 2 = 518.

Note that an earlier iteration of this page contained an incorrect description of the algorithm which would give the same SP-ID for both of the following two positions.

```
   RQNBBKRN = 601
   RNQBBKRN = 617

```
assistant
```
sub c960-spid($array) {
    # standardize on letters for easier processing
    my $ascii = $array.trans('♜♞♝♛♚♖♘♗♕♔' => 'RNBQK');

    # error-checking
    my %Names = <Q Queen K King R Rook N Knight B Bishop>;
    return 'Illegal position: should have exactly eight pieces' unless 8 == $ascii.chars;
    return 'Illegal position: Bishops not on opposite colors.'  unless 1 == sum $ascii.indices('B').map(* % 2);
    return 'Illegal position: King not between rooks.'          unless $ascii ~~ /'R' .* 'K' .* 'R'/;
    for <K 1 Q 1 B 2 N 2 R 2> -> $piece, $count {
        return "Illegal position: should have exactly $count %Names{$piece}\(s\)\n" unless $count == $ascii.indices($piece)
    }

    # Work backwards through the placement rules.
    # King and rooks are forced during placement, so ignore them. 

    # 1. Figure out which knight combination was used:
    my @knights = $ascii.subst(/<[QB]>/, '', :g).indices('N');
    my $knight = combinations(5,2).kv.grep( -> $i, @c { @c eq @knights } ).flat.first;

    # 2. Then which queen position:
    my $queen = $ascii.subst('B', '', :g).index('Q'); 

    # 3. Finally the two bishops:
    my @bishops = $ascii.indices('B');
    my ($dark,$light) = (@bishops.first %% 2 ?? @bishops !! @bishops.reverse) Xdiv 2;

    $ascii.trans('RNBQK' => '♖♘♗♕♔') ~ ' ' ~ 4 × (4 × (6 × $knight + $queen) + $dark) + $light;
}

say .&c960-spid for <♖♘♗♕♔♗♘♖ ♛♞♜♝♝♞♚♜ RQNBBKRN RNQBBKRN QNBRBNKR>;

```

Output:

```
♖♘♗♕♔♗♘♖ 518
♕♘♖♗♗♘♔♖ 105
♖♕♘♗♗♔♖♘ 601
♖♘♕♗♗♔♖♘ 617
Illegal position: Bishops not on opposite colors.
```