Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 2
improved the table
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (Node.js), 84 bytes

s=>[a=~"0628"[[C,P,F,R]=Buffer(s),i=P%11%7],b=-"63682"[i]||4][C&1]+(F+R&1?45-a-b:19)

Try it online!

How?

The color, the piece, the file and the rank characters are turned into ASCII codes which are saved into \$C\$, \$P\$, \$F\$ and \$R\$ respectively.

We compute the piece index \$i=(P \bmod 11)\bmod 7\$:

 char. | code | mod 11 | mod 7
-------+------+--------+-------
 'B' | 66 | 0 | 0 Reordered by index:
 'K' | 75 | 9 | 2 
 'N' | 78 | 1 | 1 0 | 1 | 2 | 3 | 4 | 5
 'P' | 80 | 3 | 3 ---+---+---+---+---+---
 'Q' | 81 | 4 | 4 B | N | K | P | Q | R
 'R' | 82 | 5 | 5

We compute \$a\$, which is the number of outline squares for the piece, minus \19ドル\$:

a = ~"0628"[i]

We compute \$b\$, which is the number of filled squares for the piece, minus \19ドル\$:

b = -"63682"[i] || 4

We use C & 1 to figure out the color of the piece and F + R & 1 to figure out the color of the square.

The final result is:

[a, b][C & 1] // use outline squares if the piece is black
+ // or filled squares if the piece is white
( //
 F + R & 1 ? // if the square is white:
 45 - a - b // add 19 + (64 - ((a + 19) + (b + 19)))
 // = 45 - a - b
 : // else:
 19 // just add 19
) //
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

AltStyle によって変換されたページ (->オリジナル) /