Output all the white or black squares of a chessboard
Pyth, 22 21 bytes
-1 byte by @Sp3000
fn%Chz3%sCMT2sM*<G8S8
Under the function %Chz3
, dark
hashes to 1, light
to 0, and both
to 2. If we take the parity of the sum of the ords of a chess square (that is, a1
-> [97, 33]
-> (97 + 33)%2
= 0
, dark squares go to 0, and light to 1. This allows us to filter by inequality.
fn%Chz3%sCMT2sM*<G8S8 implicit: z=input
* Cartesian product of
<G8 first 8 letters in G (alphabet)
S8 with [1,...,8] implicitly stringified
sM*<G8S8 ['a1','a2,...,'a8','b1'...'h8']
f T Filter that by gives truthy result to lambda T:
sCMT The sum of the ords of the chars in T,
% 2 modulo 2
n does not equal
Chz ord of the first char in z,
% 3 modulo 3
Implicitly print the list.
Try it here.
Bash + GNU Utilities, 74
printf %s\\n {a..h}{1..9}|sed -n "`sed '/[db]/a1~2p
/t/a2~2p
c/9/d'<<<$1`"
{a..h}{1..9}
is a bash brace expansion that produces all the coordinates for an 8x8 board, plus an extra column 9
. This is important because it makes the row length odd which allows the chequerboard effect.
The printf
simply formats each coordinate, one per line.
The built sed expression then deletes all x9
coordinates and then prints either even or odd or both input lines, according to the script input.
JavaScript (SpiderMonkey 30+), 90 85 83 82 bytes
x=>[for(d of"12345678")for(c of"abcdefgh")if(x>'l'^parseInt(c+=d,19)%2|x<'d')c]+''
Returns a comma-separated string of squares. Compatible version for 99 bytes:
x=>([..."12345678"].map(d=>[..."abcdefgh"].map(c=>c+d).filter(s=>x>'l'^parseInt(s,19)%2|x<'d')))+''
Works by enumerating all 64 square names, then parsing them in base 19 to see whether they are light or dark modulo 2.