Capture on the Pawn Chessboard
Retina, 33 29 bytes
T`d`w
)`\d
$0.
_
p.{7}(..)?P
To run the code from a single file, use the -s
flag.
Should be easily beatable by something like Perl where the expansion of digits into strings of spaces (or other characters) doesn't take up 17 bytes.
The output is positive (truthy) if there is a possible capture and zero (falsy) if there isn't.
Explanation
T`d`w
)`\d
$0.
This is a loop of two stages. The first one is a transliteration stage which decrements each digit, and turns zeroes to underscores. Why? Because d
and w
expand to the following two lines:
0123456789
_0123456789AB...YZab...yz
If the target set of a transliteration stage is longer than the source set, the extraneous characters are ignored, hence the decrementing behaviour (honestly, it was just luck that I decided to put the underscore in front of the digits when expanding the w
character class).
Then the second stage is a replacement, which appends a .
to each digit. That means that for each digit n
, n
periods are added before that digit is turned into an underscore.
_
<empty>
This just gets rid of the underscores.
p.{7}(..)?P
Finally, we find the matches. Since we're ignoring en passant, captures are only possible if there is a p
and then a P
diagonally below it. In the linear string, this simply means that there must be 7 or 9 characters between the two pawns. This matched with .{7}(..)?
(i.e. match 7 characters and then, optionally, match another two).
Such a match stage returns the number of matches it found.
Pyth, 25 bytes
/smC,>JsXz`M9*LN9dJ,8T"Pp
Steps:
Transform the input by replacing the digits with the equivalent number of quote marks (N
). This is saved in J
. We then slice off the first 8 or 10 characters, and zip the result with the original. Any capturing pair will be transformed into "Pp"
, so we then find the count of that string in the resultant list. This is the output.
As a bonus, this actually counts the number of captures possible in the input.
Javascript, 272 characters
function h(t){b=[[]];for(i=-1;i++<7;){c=0;b.push(l=[]);for(j=-1;j++<7;){o=t.split('/')[i][j];switch(o){case'P':l[c++]=-1;break;case'p':l[c++]=1;break;default:c+=parseInt(o);}}}b.push([]);for(i=1;i<9;i++)for(j=0;j<8;j++)if((p=b[i][j])&&(b[i+p][j-1]||b[i+p][j+1]))return 1;}
There's probably a lot of room for improvement.