2
\$\begingroup\$

Input

A single string, which contains no whitespace, as a command-line argument.

Output

If the string is not a valid algebraic notation pawn promotion, produce no output and exit. If the string is a valid pawn promotion, convert it to ICCF numeric notation and print the result, followed by a newline.

A valid algebraic notation pawn promotion consists of:

  • A letter from a to h indicating the pawn's departure column (known as a "file" in chess);
  • Optionally, "x" followed by the file of the pawn's arrival square if it has captured a piece in promoting;
  • "1" or "8" depending on which side promoted; and
  • "=" followed by one of "Q", "R", "B", "N" to indicate the chosen piece;
  • Optionally, "+" to indicate the move was a check or "#" to indicate the move was a checkmate.

An valid ICCF numeric notation pawn promotion consists of:

  • Two digits indicating the coordinates of the square from which the pawn departed (the files are numbered and not lettered as in algebraic notation);
  • One digit indicating the file on which the pawn arrived, in case it has captured a piece while promoting (this does not officially comply with the standard, but I prefer keeping all moves as 4 digits for simplicity); and
  • One digit to indicate the chosen piece: 1 for queen, 2 for rook, 3 for bishop, 4 for knight.

Examples

e1=Q   => 5251
cxd8=B => 3743
axh8=R => no output (a-pawns cannot capture on h-file)
h8=N+  => 8784
\$\endgroup\$
  • \$\begingroup\$ Could we have some test cases? \$\endgroup\$ – Doorknob Mar 15 '16 at 2:56
  • \$\begingroup\$ @Doorknob Added. \$\endgroup\$ – EMBLEM Mar 15 '16 at 3:00
  • \$\begingroup\$ Can we exit with an error if the input is invalid? Does # for checkmate need to be supported? \$\endgroup\$ – Doorknob Mar 15 '16 at 3:02
  • \$\begingroup\$ @Doorknob No, you must produce no output and exit without errors. # does need to be supported. \$\endgroup\$ – EMBLEM Mar 15 '16 at 3:03
  • \$\begingroup\$ Why the input restriction? \$\endgroup\$ – CalculatorFeline Jun 21 '17 at 3:23
1
\$\begingroup\$

JavaScript (ES6), 166 bytes

s=>(g=n=>parseInt(m[n][0],19)-9,m=s.match(/^([a-h]x)?([a-h][18]=[QRBN])[+#]?$/))?m[1]&&g(2)+~g(0)&&g(0)+~g(2)?'':''+g(0)+(m[2][1]-1|2)+g(2)+" QRBN".search(m[2][3]):''

The validation makes this horribly long. Without validation, it's only 108 bytes:

s=>s.replace(/(.x)?(..=.).*/,([a],_,[c,d,,f])=>g(a)+(d-1|2)+g(c)+" QRBN".search(f),g=f=>parseInt(f,19)-9+'')
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.