Output all the white or black squares of a chessboard

29

1

Introduction

This is how a chessboard looks like.

enter image description here

You can see that a1 is a dark square. However, b1 is a light square.

The Task

The challenge is, given dark, light or both, output all the dark, light or all squares with a separator (like a whitespace or a newline). The order of all the squares does not matter.

Test cases

Input: dark
Output: a1 a3 a5 a7 b2 b4 b6 b8 
        c1 c3 c5 c7 d2 d4 d6 d8 
        e1 e3 e5 e7 f2 f4 f6 f8 
        g1 g3 g5 g7 h2 h4 h6 h8

Input: light
Output: a2 a4 a6 a8 b1 b3 b5 b7 
        c2 c4 c6 c8 d1 d3 d5 d7 
        e2 e4 e6 e8 f1 f3 f5 f7 
        g2 g4 g6 g8 h1 h3 h5 h7

Input: both
Output: a1 a2 a3 a4 a5 a6 a7 a8
        b1 b2 b3 b4 b5 b6 b7 b8
        c1 c2 c3 c4 c5 c6 c7 c8
        d1 d2 d3 d4 d5 d6 d7 d8
        e1 e2 e3 e4 e5 e6 e7 e8
        f1 f2 f3 f4 f5 f6 f7 f8
        g1 g2 g3 g4 g5 g6 g7 g8
        h1 h2 h3 h4 h5 h6 h7 h8

Note: I have prettified the output but this is not necessary.

This is , so the submission with the least amount of bytes wins!

Adnan

Posted 2016-02-16T18:40:12.733

Reputation: 41 965

So, something like a2a4a6... would be okay? – Conor O'Brien – 2016-02-16T19:16:27.977

@CᴏɴᴏʀO'Bʀɪᴇɴ It does have to contain a seperator, like a whitespace or a newline, so that is invalid. – Adnan – 2016-02-16T19:17:20.093

Can we output a raw 2d matrix? I.e. [[a2,a4,a6,a8],[...]...] – Conor O'Brien – 2016-02-16T19:33:27.793

@CᴏɴᴏʀO'Bʀɪᴇɴ Yes, that is allowed – Adnan – 2016-02-16T19:34:42.937

Do light,dark and both have to be input as Strings or can they be represented via any data type? – W.K.S – 2016-02-16T20:33:48.180

@W.K.S The input is always light, dark or both. What you do with these values is up to you. – Adnan – 2016-02-16T21:19:34.877

@Adnan So outputs other than light dark or both can have undefined, mean behavior? – corsiKa – 2016-02-18T22:13:26.263

@corsiKa Yes, that is correct. – Adnan – 2016-02-18T22:14:06.173

Answers

15

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.

lirtosiast

Posted 2016-02-16T18:40:12.733

Reputation: 20 331

21: fn%Chz3%sCMT2sM*<G8S8 – Sp3000 – 2016-02-17T10:27:25.353

@Sp3000 Thanks! Knowing I was using 6 bytes to get it to fit, I should have tried different hashes. – lirtosiast – 2016-02-18T05:25:55.823

13

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.

Digital Trauma

Posted 2016-02-16T18:40:12.733

Reputation: 64 644

11

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.

Neil

Posted 2016-02-16T18:40:12.733

Reputation: 95 035

Good. This is ES7 – edc65 – 2016-02-16T19:49:59.403

@edc65 Ah, I couldn't remember. I take it my second version is "only" ES6. – Neil – 2016-02-16T21:15:30.643

Now ES6 beats ES7 – edc65 – 2016-02-16T23:00:19.697

@edc65 You were saying? – Neil – 2016-02-17T00:50:08.180

It's really terse - beautiful! – edc65 – 2016-02-17T21:20:16.217

... but ES6 is at 82 now – edc65 – 2016-02-17T22:17:30.227

4@edc65 I don't suppose we could agree to a draw? – Neil – 2016-02-17T22:36:12.307

Let us continue this discussion in chat.

– edc65 – 2016-02-17T22:38:24.347

10

JavaScript (ES6), 82 87 98

Anonymous function returning a space separated string of squares.

i=>eval("for(o='',v=190;v<322;)v++%19<8&&i<'d'|v&1^i>'l'?o+=v.toString(19)+' ':o")

TEST

f=i=>eval("for(o='',v=190;v<322;)v++%19<8&&i<'d'|v&1^i>'l'?o+=v.toString(19)+' ':o")

// less golfed

q=i=>{
  // loop over the range of number a0 (base 19) to h8 (base 19)
  for(o='',v=190;v<322;) 
  {
    if (v++ %19 < 8) // increment and execute the line below only if second digit in 1..8
      if (i<'d'|v&1^i>'l') // even == light, odd == dark, take both if input is 'both'
        o+=v.toString(19)+' '
  }
  return o
}

document.write('<hr>Both<br>'+f('both'))
document.write('<hr>Light<br>'+f('light'))
document.write('<hr>Dark<br>'+f('dark'))

edc65

Posted 2016-02-16T18:40:12.733

Reputation: 31 086

1Wow... that's just crazy! I wonder if it's possible to get any shorter with ES6... – ETHproductions – 2016-02-17T21:11:21.077

@ETHproductions yes it is! I have an 86 ready, but I'm still trying to do something better (my - moving - target is Neil with 85 ... no damn 83) – edc65 – 2016-02-17T21:18:25.257

7

Batch, 192 bytes

@set s=a1 a3 a5 a7
@set t=b2 b4 b6 b8
@if not %1==light call:b
@set s=a2 a4 a6 a8
@set t=b1 b3 b5 b7
@if %1==dark exit/b
:b
@echo %s% %s:a=c% %s:a=e% %s:a=g% %t% %t:b=d% %t:b=f% %t:b=h%

Neil

Posted 2016-02-16T18:40:12.733

Reputation: 95 035

4

Pyth, 48 39 bytes

K*<G8S8Jfq%xGhT2%seT2K?qhz\bK?qhz\lJ-KJ

Try it here!

Still longer than the other Pyth solution, but I don't think I can beat this with my algorithm.

Explanation

First we generate a list of all squares on the board and assign it to Y. Then we filter this list so that only light squares remain and assign this list to J. After that we evaluate the input and print:

  • Y if input was both
  • J if input was light
  • Y-J if the input was dark

Determining if a square is light works as follows:

  • Map the char to a number from 1-8 (a->1, b->2), results in 18 for a8, etc.
  • check if both those numbers are odd or even (x%2 == y%2)
  • If they are, the square is light, otherwise its dark

K*&ltG8S8Jfq%xGhT2%seT2K?qhz\bK?qhz\lJ-KJ  # z=input

 *                                         # Cartesian product of
  &ltG8                                      # first 8 letters of the alphabet (a-h)
     S8                                    # 1-indexed range (1-8)
K                                          # K holds now all squares
       f             K                     # Filter K 
        q                                  # is equal
         %xGhT2                            # map [a-h] to a number [1-8] and take it modulo 2
               %seT2                       # Take modulo 2 from the row number
                      ?qhz\bK              # If input starts with 'b' print K
                             ?qhz\lJ       # If it starts with 'l' print J
                                    -KJ    # Otherwise print the difference of those 2

Denker

Posted 2016-02-16T18:40:12.733

Reputation: 6 639

Oh geez that's shorter than mine by a long shot. – Addison Crump – 2016-02-16T20:26:15.797

4

PHP, 132 126 120 108 106 bytes

for($s=strtr($argv[1],bdl,210);$c<8;$c++)for($r=0;$r<8;)if((++$r+$c)%2==$s||$s>1)echo"abcdefgh"[$c]."$r ";

It loops through the cols (0-7) and rows (1-8) and checks if the sum of both is odd/even.

Tested with PHP 5.6.4, run it: php -d error_reporting=30709 -r '<CODE>' {dark|light|both}

killerbees19

Posted 2016-02-16T18:40:12.733

Reputation: 41

1Welcome to PPCG! This is a good answer, but you'll get more votes if you add an explanation. – lirtosiast – 2016-02-16T20:47:17.903

I think you can replace$s==2 with $s-1. If $s=2, and -1, it's 1, which is truthy and will continiue – Martijn – 2016-02-17T08:12:15.053

And I think $c=0 can be $c, it'll give a bunch of notices, but at least for dark it works fine – Martijn – 2016-02-17T08:42:18.807

Thank you, Martijn! I forgot to remove the braces too, -6 bytes for now. And I don't know why, but $s-1 doesn't work, but it should. Thanks for this great idea! I'll debug that later. – killerbees19 – 2016-02-17T11:23:39.340

I'm new to this site, but error messages because of undefined $c variable? That sounds a bit strange and invalid. Or not? – killerbees19 – 2016-02-17T11:26:12.713

Nope that's perfectly valid. It's only a notice, not really an error, as it will interpret the unset variable as a null. You can mute the error messages with php -d error_reporting=30709. Also, here are some tips for PHP golfs. I only do PHP answers, so you might have a look at my answers for some inspiration as well.

– aross – 2016-02-17T13:33:26.157

You can also drop the incrementer in the $r loop and move it to the first or last use of $r. Just test the polarity on it. – Not that Charles – 2016-02-17T14:45:50.520

Also, I don't know PHP, but maybe you can not declare $i and just move it inline where you use it? – Not that Charles – 2016-02-17T14:47:52.943

Inline $i works, great idea! $r incrementer moved. $c definition removed. -12 bytes – woohoo! :-) – killerbees19 – 2016-02-17T16:20:12.553

@Martijn $s-1 does not work, because "light" evaluates as 0 - 1 = -1 and that is true too! – killerbees19 – 2016-02-17T16:30:57.123

Here you go: for($s=strtr($s[0],bdl,210);$c<8;$c++)for($r=0;$r<8;)if((++$r+$c)%2==$s||$s>1)echo'abcdefgh'[$c],"$r "; (103 bytes). The replacements are easy to find. – Ismael Miguel – 2016-02-17T16:39:23.760

By the way, how are you taking input? This is neither a full program nor a function, and register globals was removed in PHP 5.4. – aross – 2016-02-17T16:42:03.157

you shouldn't set error_reporting to 0, because that also hides fatal errors. 30709 just hides notices, warnings and strict – aross – 2016-02-17T16:51:13.873

Better now? It takes the first CLI argument. Thx Ismael and Aross for your input! – killerbees19 – 2016-02-17T16:51:31.277

Looks much better, +1. I challenge you to improve upon my score though :)

– aross – 2016-02-17T17:10:12.690

btw @aross: Why print instead of echo? It's one extra byte. (I post it here because I don't have 50 reputation to comment on your solution, which is a brilliant and short implementation!) – killerbees19 – 2016-02-17T22:59:47.413

echo is a language construct and cannot be used in such a context, whereas print can. – aross – 2016-02-18T00:19:54.170

Oh indeed. Stupid me… – killerbees19 – 2016-02-18T15:04:20.003

4

Python 2, 73 71 70 bytes

lambda s:[chr(x/8+97)+`x%8+1`for x in range(64)if x+x/8&1^ord(s[0])%3]

I'm still a bit confused whether functions are okay for the question, since the challenge mentions a "separator", but since there's a lot of other function submissions I've done the same.

Similar to Erwan's answer but with a lot bit more Python 2-ness.

(-2 bytes thanks to @xnor)

Sp3000

Posted 2016-02-16T18:40:12.733

Reputation: 58 729

lol I don't even test between s=="dark" and s[0]=="d" but for my defence in my really first try i used s,*_=s and 4 cmp – Erwan – 2016-02-17T09:58:32.907

1I feel like there should be something shorter like ord(s[_])&_ or ord(s[_])/_. – xnor – 2016-02-17T10:02:45.983

@xnor Indeed, there is with % :) Thanks! – Sp3000 – 2016-02-17T10:09:24.613

3

Vitsy, 90 82 bytes

'`'8\[1+8\:]Yy1-\?8\['1'v8\[vD1+vr?]vX]i'h'-)[88*\[Z?aO]]i'r'-)[?1m]1m
84*\[Z??aO]

Explanation of the first line:

'`'8\[1+8\:]Yy1-\?8\['1'v8\[vD1+vr?]vX]i'h'-)[88*\[Z?aO]]i'r'-)[?1m]i'g'-)[1m]
'`'                     Push ` to the stack. (this is 1 less than a in ASCII)
   8\[     ]            Do the stuff in brackets 8 times.
      1+                Add one on every recursion (this gets a, b, c, d...)
        8\:             Clone the stack 8 times. (This gets 8 of each a, b, c...)
Y                       Remove the current stack.
 y1-\?                  Go one stack to the left (I really need to builtin this)
8\[                 ]   Do the stuff in brackets 8 times.
   '1'                  Push character literal 1 to the stack.
      v                 Save it as a temporary variable.
       8\[       ]      Do the stuff in brackets 8 times.
          v             Push the temporary variable to the stack.
           D            Duplicate the top item of the stack.
            1+          Add one to it (this gives us 1, 2, 3, 4...)
              v         Capture the top item of the stack as a temporary variable.
               r        Reverse the stack.
                ?       Go a stack to the right.
                  vX    Clear the temporary variable slot.
i'h')[          ]       If the last character of the input is 'h', do the stuff in brackets
      88*\[    ]        Do the stuff in brackets 64 times.
           Z            Output everything in the stack as a character.
            ?           Rotate right a stack.
             aO         Output a newline.
i'r')[?1m]              If the penultimate character of the input is 'r', rotate over a 
                        stack, then execute the first index of lines of code.
1m                      Execute the first index of lines of code.

Explanation of the second line:

84*\[Z??aO]
84*\[     ]   Do the stuff in brackets 32 times.
     Z        Output everything in the stack as a char.
      ??      Rotate two stacks over.
        aO    Output a newline.

There will be bonus trailing newlines for 'dark' and 'both'. Requires that only 'dark', 'both', or 'light' will be input.

Try it online!

Addison Crump

Posted 2016-02-16T18:40:12.733

Reputation: 10 763

3

PowerShell v3+, 142 129 bytes

param($a)$d=$a[0]-in('d','b');$l=$a[0]-in('l','b')
97..104|%{$i=[char]$_;1..8|%{if((($q=($_+$i)%2)-eq$l)-or($q+1-eq$d)){"$i$_"}}}

Takes input $a and sets two variables for if we're to output $dark or $light squares based on the first letter of the input.

Then, we loop over a-h and 1-8 and uses the same trick as on Determine the color of a chess square to parse whether it's a light or dark square (setting helper variable $q in the first test) and add that square to the pipeline if appropriate. After execution, the elements on the pipeline are output one per line.

Requires v3 or newer for the -in operator.

Edit - Saved 13 bytes by eliminating the switch and by changing equality testing order

AdmBorkBork

Posted 2016-02-16T18:40:12.733

Reputation: 41 581

3

Jolf, 48 bytes

Ζ-ώ~1tΜ fΜZAQ8ΨΖ+ζ|<%ζγwώt8ώ6d|<i'd!x%H2>i'ldbHγ

It's all greek to me ¯\_(ツ)_/¯ This is a transpiling of edc65's excellent answer.

Ζ-ώ~1t
Ζ        set ζ to 
  ώ~1     100 * 2
 -   t    minus 10 (=190)

ΜZAQ8ΨΖ+ζ|<%ζγwώt8+2t
 ZAQ8                 A zero array of length Q8 (8*8 = 64)
Μ    Ψ                map that
      Ζ+ζ             ζ += 
           %ζγwώt       ζ % (γ = 19)
          <      8      < 8
         |        ώ6  || 12

Μ f■above thing■d|<i'd!x%H2>i'ldbHγ
 _f■above thing■d                    filter the above thing
                 |<i'd!x%H2>i'l      removing all the bad stuff (i<'d'|v%2^i>'l')
Μ                              dbHγ  map each character to base 19

Conor O'Brien

Posted 2016-02-16T18:40:12.733

Reputation: 36 228

3

C++, 132 bytes

Takes input by command-line. Uses pointer/modulo voodoo for print condition.

#include<stdio.h>
int main(int i,char**v){for(int n=0;n<64;n++)if((n+(i=n/8))%2-*v[1]%3){putchar(i+97);putchar(n%8+49);putchar(32);}}

MegaTom

Posted 2016-02-16T18:40:12.733

Reputation: 3 787

I don't think the n-loop is necessary. I think nested for loops for i and j would trim a few bytes off. The (i+j)%2 approach is really clever. I hadn't thought of that. – W.K.S – 2016-02-17T07:48:34.387

I just notice that (i//8+i%8)%2 is the same as (i//8+i)%2 so you can win some bytes if you remove the definition of j=n%8 – Erwan – 2016-02-17T09:03:22.993

3

PHP, 99 82 79 76 74 73 bytes

Uses ISO 8859-1 encoding.

for($z=$argv[1];++$x<72;)$x%9&&$z<c|$z>k^$x&1&&print~ß.chr($x/9+97).$x%9;

Run like this (-d added for aesthetics only):

php -d error_reporting=30709 -r 'for($z=$argv[1];++$x<72;)$x%9&&$z<c|$z>k^$x&1&&print~ß.chr($x/9+97).$x%9; echo"\n";' dark

It works like this: variable $x is incremented from 1 to 71, the numbers correspond to the cells as shown below.

r\c 1  2  3  4  5  6  7  8  [invalid column]
A   1  2  3  4  5  6  7  8  9
B  10 11 12 13 14 15 16 17 18
C  19 20 21 22 23 24 25 26 27
D  28 29 30 31 32 33 34 35 36
E  37 38 39 40 41 42 43 44 45
F  46 47 48 49 50 51 52 53 54
G  55 56 57 58 59 60 61 62 63
H  64 65 66 67 68 69 70 71 72

Therefore, $x modulo 9 yields the column number and $x / 9 yields the row number, which I convert to a letter using chr. The code $z<c|$z>k^$x&1 yields true for input both ($z<c) and in the case of light or dark only for the even or odd cells respectively ($z>k ^ $x&1). The result of this expression determines whether or not the cell coordinates will then be printed. Finally, if $x modulo 9 results in 0, I skip that non-existant cell.

  • Saved 18 17 bytes (fixed a bug) by having only 1 loop, converting the number to a char instead of the other way around
  • Saved 3 bytes by combining the condition for dark and light with a xor
  • Saved 3 bytes by comparing against the full input instead of the first char
  • Saved 2 bytes because no longer need to subtract .125 in the expression $x/9+69.9 to get the correct row number before converting to a char
  • Saved a byte by using to yield a space

aross

Posted 2016-02-16T18:40:12.733

Reputation: 1 583

3

Perl, 69 + 3 = 72 bytes

$b=/b/;$i=/l/;$_="@{[grep{$i=!$i||$b}map{$l=$_;map{$l.$_}1..8}a..h]}"

To be run with perl -p, for which I've added 3 bytes.

Less-golfed version (slightly different, as the babycart operator makes it hard to format nicely):

$b=/b/;                       # flag for input containing b
$i=/l/;                       # start $i as true if input contains 'l'

@a = grep {
    $i = !$i||$b                # alternate unless $b is true
} map {
    $l = $_;                    # save letter
    map {
        $l.$_                   # join letter and number
    } 1..8                      # generate number sequence
} a..h;                         # generate letter sequence

# golfed version uses babycart operator around array expr to save one byte
$_ = "@a"                       # write array, separated

The golfed version uses "@{[]}"; the commented version uses @a=...; "@" so that the commented code is still runnable.

David Morris

Posted 2016-02-16T18:40:12.733

Reputation: 241

map$l.$_,1..8 -1 – choroba – 2016-02-18T17:19:00.347

and the same trick for grep: grep$i=!$i||$b,map again -1 – choroba – 2016-02-18T17:22:14.243

3

Java, 143

class H{public static void main(String[]a){for(char
c=96;++c<'i';)for(int
i=0;++i<9;)if((i+c)%2!=a[0].charAt(0)%3)System.out.println(c+""+i);}}

Hey, it's not the longest answer :)

Input is taken as a command-line argument.

aditsu quit because SE is EVIL

Posted 2016-02-16T18:40:12.733

Reputation: 22 326

2

JavaScript ES6, 187 160 159 bytes

I'm probably missing something painfully obvious. Oh well. Not having to flatten the array helps.

l=s=>(E=[2,4,6,8],O=[1,3,5,7],h=(z=s[0]=="d")?O:E,d=z?E:O,[...h.map(t=>[..."aceg"].map(e=>e+t)),...(d.map(t=>[..."bdfh"].map(e=>e+t))),...(s[0]=="b"?l`d`:[])])

Returns a 2D array.


Try it here:

l=s=>(E=[2,4,6,8],O=[1,3,5,7],h=(z=s[0]=="d")?O:E,d=z?E:O,[...h.map(t=>[..."aceg"].map(e=>e+t)),...(d.map(t=>[..."bdfh"].map(e=>e+t))),...(s[0]=="b"?l`d`:[])])

U=x=>o.innerHTML=JSON.stringify(l(i.value));
i.onchange=U;U();
*{font-family:Consolas,monospace;}
<select id=i><option value="light">light</option><option value="dark">dark</option><option value="both">both</option></select><div id=o></div>

Conor O'Brien

Posted 2016-02-16T18:40:12.733

Reputation: 36 228

2

Rust, 263 259 244 Bytes

use std::char;use std::env;fn main(){let n=env::args().nth(1).unwrap();for i in 0..8{for j in 0..8{if n=="both"||(n=="dark"&&(i+j)%2==0)||(n== "light"&&(i+j)%2!=0){println!("{}{}",char::from_u32(i+97).unwrap(),char::from_u32(j+49).unwrap())}}}}

Expanded Form:

fn main() {
    let input = env::args().nth(1).unwrap();
    for i in 0..8{
            for j in 0..8{
                if input == "both"
                || (input == "dark" && (i+j)%2==0)
                || (input == "light" && (i+j)%2!=0){
                    println!("{}{}",char::from_u32(i+97).unwrap(),char::from_u32(j+49).unwrap());
            }
        }
    }
}

W.K.S

Posted 2016-02-16T18:40:12.733

Reputation: 121

1Rather than hard-coding your input, is it not possible to read it from the terminal or the command line or as a function parameter? – Neil – 2016-02-17T00:52:25.687

2

R, 129 94 bytes

I knew I could generate the board better :). Essentially this builds an inverted board, filtering out grid references where the shade does not match the input. Output is space separated.

a=which(array(c('light','dark'),c(9,9))[-9,-9]!=scan(,''),T);cat(paste0(letters[a[,1]],a[,2]))

Ungolfed

a=which(                           # Get the indexes of
  array(c('light','dark'),c(9,9))  # an array of light dark
    [-9,-9]                        # except for the ninth row and column
      !=scan(,'')                  # where the value doesn't equal the input
    ,T                             # return array index not vector
  );
cat(paste0(letters[a[,1]],a[,2]))  # using letters for col

Test

> a=which(array(c('light','dark'),c(9,9))[-9,-9]!=scan(,''),T);cat(paste0(letters[a[,1]],a[,2]))
1: dark
2: 
Read 1 item
a1 c1 e1 g1 b2 d2 f2 h2 a3 c3 e3 g3 b4 d4 f4 h4 a5 c5 e5 g5 b6 d6 f6 h6 a7 c7 e7 g7 b8 d8 f8 h8
> a=which(array(c('light','dark'),c(9,9))[-9,-9]!=scan(,''),T);cat(paste0(letters[a[,1]],a[,2]))
1: light
2: 
Read 1 item
b1 d1 f1 h1 a2 c2 e2 g2 b3 d3 f3 h3 a4 c4 e4 g4 b5 d5 f5 h5 a6 c6 e6 g6 b7 d7 f7 h7 a8 c8 e8 g8
> a=which(array(c('light','dark'),c(9,9))[-9,-9]!=scan(,''),T);cat(paste0(letters[a[,1]],a[,2]))
1: both
2: 
Read 1 item
a1 b1 c1 d1 e1 f1 g1 h1 a2 b2 c2 d2 e2 f2 g2 h2 a3 b3 c3 d3 e3 f3 g3 h3 a4 b4 c4 d4 e4 f4 g4 h4 a5 b5 c5 d5 e5 f5 g5 h5 a6 b6 c6 d6 e6 f6 g6 h6 a7 b7 c7 d7 e7 f7 g7 h7 a8 b8 c8 d8 e8 f8 g8 h8
>

MickyT

Posted 2016-02-16T18:40:12.733

Reputation: 11 735

2

Oracle SQL 11.2, 192 180 bytes

SELECT CHR(64+x),DECODE(y,0,8,y)FROM(SELECT CEIL(LEVEL/8)x,MOD(LEVEL,8)y FROM DUAL CONNECT BY LEVEL<=64)WHERE(:1='dark'AND MOD(x+y,2)=0)OR(:1='light'AND MOD(x+y,2)=1)OR(:1='both');

Un-golfed

WITH v AS
(
  SELECT CEIL(LEVEL/8)x, DECODE(MOD(LEVEL,8),0,8,MOD(LEVEL,8))y  
  FROM DUAL CONNECT BY LEVEL<=64
)
SELECT CHR(64+x),y
FROM   v
WHERE  (:1='dark' AND MOD(x+y,2)=0)OR(:1='light' AND MOD(x+y,2)=1)OR(:1='both');

The v view generate the coordinates of each square. If the sum of the coordinates is even then the square is black, else it's white.

Jeto

Posted 2016-02-16T18:40:12.733

Reputation: 1 601

2

MATL, 31 bytes

1)t3\8:t!++w4\~?H\]2#f2Y2!w)wVh

Try it online!

Luis Mendo

Posted 2016-02-16T18:40:12.733

Reputation: 87 464

This one doesn't seem to give the correct squares. "dark" is giving x1,x3,x5,x7 for every letter x, but that corresponds to 4 columns, not the black squares. – Esteemator – 2016-02-17T14:10:15.210

@Esteemator Sorry, my mistake. Corrected – Luis Mendo – 2016-02-17T14:52:13.907

2

Ruby, 85

I think there are shorter ways about this, but this is a cute use of .upto.

gets;'a1'.upto('h8'){|e|puts e if e[/[1-8]/]&&(~/b/||((e.ord%2!=e[1].ord%2)^! ~/l/))}

Not that Charles

Posted 2016-02-16T18:40:12.733

Reputation: 1 905

2

Haskell, 133 116 105 100 98 91 bytes

f r=[["abcdefgh"!!x,"12345678"!!y]|x<-l,y<-l,odd(x+y)||r<"l",even(x+y)||r!!0/='d']
l=[0..7]

This is my first attempt at golfing Haskell.

With some help from Michael Klein, we managed to get it under 100 chars!

joeytwiddle

Posted 2016-02-16T18:40:12.733

Reputation: 601

1How about c>0 for c==1 and c<1 for c==0? Saves two bytes. – Michael Klein – 2016-02-19T06:39:42.783

Fantastic, we got it under 100! Thank you Michael. – joeytwiddle – 2016-02-19T06:48:39.617

1You're welcome. I got a bit sucked in and got it down to 86 bytes by refactoring a bit: f r=[[[a,b]|a<-['a'..'h'],b<-['1'..'8']]!!i|i<-[0..63],even i||r<"l",odd i||r!!0/='d'] – Michael Klein – 2016-02-19T07:21:38.020

1That's very nice, a rethought approach. Although I'm sorry to say that odd and even i do not give us diagonal stripes. Some solve this with i+i\div`8(likex+y). Others start with['1'..'9']and[0..71]and then retain only thei`mod`9<8results later, for 96 bytes. However, this hybrid of our two approaches does well at 91 bytes:l=[0..7];f r=[["abcdefgh"!!x,"12345678"!!y]|x<-l,y<-l,odd(x+y)||r<"l",even(x+y)||r!!0/='d']` – joeytwiddle – 2016-02-19T09:28:40.503

Ah, well that's still a good bit better – Michael Klein – 2016-02-19T09:54:04.303

2

CJam, 29

qci3%:X;8Ym*{~+2%X-},"a1 "f.+

Just a quick and dirty solution :p
Try it online

Explanation:

q           read the input
ci          convert to (first) character then to integer
3%          modulo 3; results for d(ark), l(ight) and b(oth) are 1, 0, 2
:X;         store in X and pop
8Ym*        generate all pairs (Y=2) of numbers from 0 to 7
{…},        filter using the condition block
  ~         dump the current pair on the stack
  +2%       calculate the sum modulo 2
  X-        subtract X; if the result is not 0, the pair is kept
"a1 "f.+    vectorized-add "a1 " to each remaining pair
             this means the character 'a' is added to the first number,
             the character '1' is added to the second number,
             and then the space character is appended
            the contents of the stack are automatically printed at the end

aditsu quit because SE is EVIL

Posted 2016-02-16T18:40:12.733

Reputation: 22 326

1

Mathematica 133 bytes

Method 1: 108 bytes. This constructs the board as a table, with labels in each cell, and returns light or dark diagonals or bands as required.

Table[Table[{i,j},{i,{h,g,f,e,d,c,b,a}},{j,Range@8}]~Diagonal~k,{k,If[#=="light",-6,-7],7,If[#=="both",1,2]}]&

%["light"]   (*where % repeats the preceding line *)

{{{b, 1}, {a, 2}}, {{d, 1}, {c, 2}, {b, 3}, {a, 4}}, {{f, 1}, {e, 2}, {d, 3}, {c, 4}, {b, 5}, {a, 6}}, {{h, 1}, {g, 2}, {f, 3}, {e, 4}, {d, 5}, {c, 6}, {b, 7}, {a, 8}}, {{h, 3}, {g, 4}, {f, 5}, {e, 6}, {d, 7}, {c, 8}}, {{h, 5}, {g, 6}, {f, 7}, {e, 8}}, {{h, 7}, {g, 8}}}


Method 2: 133 bytes. Creates an array and selects according to the even-odd nature of the sum of the row number + column number of each cell.

Position[Array[Boole@OddQ[#+#2] &,{8,8}],Switch[#,"dark",0,"light",1,"both",0|1]]/.
{j_,k_}:>{j/.Thread[Range@8->{a,b,c,d,e,f,g,h}],k}&

DavidC

Posted 2016-02-16T18:40:12.733

Reputation: 24 524

1

JS, 197 bytes

b=[];d=[];l=[];for(i=1;i<9;i++){for(j=1;j<9;j++){a=String.fromCharCode(96+i*1)+j;b.push(a);if((i+j)%2<1){d.push(a)}else{l.push(a)}}}m=[0,"both",b,"dark",d,"light",l];alert(m[m.indexOf(prompt())+1])

Nautilus

Posted 2016-02-16T18:40:12.733

Reputation: 221

1

Python (3.5), 106 100 96 92 bytes

use the trick of MegaTom (i+j)%2 to win 6 bytes

f=lambda s:[chr(97+i//8)+str(1+i%8)for i in range(64)if s[0]=='b'or(i//8+i)%2==(s[0]=='l')]

Try it on repl.it

Results

>>> f('light')
['a2', 'a4', 'a6', 'a8', 'b1', 'b3', 'b5', 'b7', 'c2', 'c4', 'c6', 'c8', 'd1', 'd3', 'd5', 'd7', 'e2', 'e4', 'e6', 'e8', 'f1', 'f3', 'f5', 'f7', 'g2', 'g4', 'g6', 'g8', 'h1', 'h3', 'h5', 'h7']
>>> f('dark')
['a1', 'a3', 'a5', 'a7', 'b2', 'b4', 'b6', 'b8', 'c1', 'c3', 'c5', 'c7', 'd2', 'd4', 'd6', 'd8', 'e1', 'e3', 'e5', 'e7', 'f2', 'f4', 'f6', 'f8', 'g1', 'g3', 'g5', 'g7', 'h2', 'h4', 'h6', 'h8']
>>> f('both')
['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8']

Previous version

f=lambda s:[i for i in[i+j for i in'abcdefgh'for j in'123456780'][s[0]=='l'::2-(s[0]=='b')]if'0'not in i]

Erwan

Posted 2016-02-16T18:40:12.733

Reputation: 691

1

C++, 119 Bytes

Based on MegaTom's trick.

#include <stdio.h>
int main(int n,char**v){for(n=0;n<64;++n){if((n+n/8)%2-**(v+1)%3){printf("%c%c ",n/8+97,n%8+49);}}}

Johan du Toit

Posted 2016-02-16T18:40:12.733

Reputation: 1 524

0

C (gcc), 112 bytes

f(char*s){for(int m=*s^'d'?*s^'l'?3:2:1,l=64,x;l--;m&1&!x|(m&2&&x)&&printf("%c%d ",l%8+97,l/8+1))x=l%8%2^l/8%2;}

Try it online!

If a == 1, then a square will always be black if the "oddness" of row and column is the same, i.e. both are odd or both are even. The opposite is true for white squares, where row and column will always differ in oddness.

After that, it's just a matter of combining row and column loops, as well as consulting a table of operator precedence until a sufficient level of incomprehensibility has been reached.

gastropner

Posted 2016-02-16T18:40:12.733

Reputation: 3 264