29
\$\begingroup\$

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!

\$\endgroup\$
8
  • \$\begingroup\$ So, something like a2a4a6... would be okay? \$\endgroup\$ Feb 16, 2016 at 19:16
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ It does have to contain a seperator, like a whitespace or a newline, so that is invalid. \$\endgroup\$
    – Adnan
    Feb 16, 2016 at 19:17
  • \$\begingroup\$ Can we output a raw 2d matrix? I.e. [[a2,a4,a6,a8],[...]...] \$\endgroup\$ Feb 16, 2016 at 19:33
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Yes, that is allowed \$\endgroup\$
    – Adnan
    Feb 16, 2016 at 19:34
  • \$\begingroup\$ Do light,dark and both have to be input as Strings or can they be represented via any data type? \$\endgroup\$
    – W.K.S
    Feb 16, 2016 at 20:33

28 Answers 28

15
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 21: fn%Chz3%sCMT2sM*<G8S8 \$\endgroup\$
    – Sp3000
    Feb 17, 2016 at 10:27
  • \$\begingroup\$ @Sp3000 Thanks! Knowing I was using 6 bytes to get it to fit, I should have tried different hashes. \$\endgroup\$
    – lirtosiast
    Feb 18, 2016 at 5:25
13
\$\begingroup\$

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.

\$\endgroup\$
11
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ Good. This is ES7 \$\endgroup\$
    – edc65
    Feb 16, 2016 at 19:49
  • \$\begingroup\$ @edc65 Ah, I couldn't remember. I take it my second version is "only" ES6. \$\endgroup\$
    – Neil
    Feb 16, 2016 at 21:15
  • \$\begingroup\$ Now ES6 beats ES7 \$\endgroup\$
    – edc65
    Feb 16, 2016 at 23:00
  • \$\begingroup\$ @edc65 You were saying? \$\endgroup\$
    – Neil
    Feb 17, 2016 at 0:50
  • 4
    \$\begingroup\$ @edc65 I don't suppose we could agree to a draw? \$\endgroup\$
    – Neil
    Feb 17, 2016 at 22:36
10
\$\begingroup\$

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'))

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Wow... that's just crazy! I wonder if it's possible to get any shorter with ES6... \$\endgroup\$ Feb 17, 2016 at 21:11
  • \$\begingroup\$ @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) \$\endgroup\$
    – edc65
    Feb 17, 2016 at 21:18
  • \$\begingroup\$ How does the output using eval work? \$\endgroup\$ Sep 7, 2021 at 13:28
  • \$\begingroup\$ @EnderShadow8 eval returns the last expression evaluated that in this case is o in both the branches of the ternary operator \$\endgroup\$
    – edc65
    Sep 8, 2021 at 10:30
7
\$\begingroup\$

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%
\$\endgroup\$
0
4
\$\begingroup\$

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*<G8S8Jfq%xGhT2%seT2K?qhz\bK?qhz\lJ-KJ  # z=input

 *                                         # Cartesian product of
  <G8                                      # 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
\$\endgroup\$
1
  • \$\begingroup\$ Oh geez that's shorter than mine by a long shot. \$\endgroup\$ Feb 16, 2016 at 20:26
4
\$\begingroup\$

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)

\$\endgroup\$
3
  • \$\begingroup\$ 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 \$\endgroup\$
    – Erwan
    Feb 17, 2016 at 9:58
  • 1
    \$\begingroup\$ I feel like there should be something shorter like ord(s[_])&_ or ord(s[_])/_. \$\endgroup\$
    – xnor
    Feb 17, 2016 at 10:02
  • \$\begingroup\$ @xnor Indeed, there is with % :) Thanks! \$\endgroup\$
    – Sp3000
    Feb 17, 2016 at 10:09
4
\$\begingroup\$

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}

\$\endgroup\$
18
  • 1
    \$\begingroup\$ Welcome to PPCG! This is a good answer, but you'll get more votes if you add an explanation. \$\endgroup\$
    – lirtosiast
    Feb 16, 2016 at 20:47
  • \$\begingroup\$ I think you can replace$s==2 with $s-1. If $s=2, and -1, it's 1, which is truthy and will continiue \$\endgroup\$
    – Martijn
    Feb 17, 2016 at 8:12
  • \$\begingroup\$ And I think $c=0 can be $c, it'll give a bunch of notices, but at least for dark it works fine \$\endgroup\$
    – Martijn
    Feb 17, 2016 at 8:42
  • \$\begingroup\$ 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. \$\endgroup\$ Feb 17, 2016 at 11:23
  • \$\begingroup\$ I'm new to this site, but error messages because of undefined $c variable? That sounds a bit strange and invalid. Or not? \$\endgroup\$ Feb 17, 2016 at 11:26
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ map$l.$_,1..8 -1 \$\endgroup\$
    – choroba
    Feb 18, 2016 at 17:19
  • \$\begingroup\$ and the same trick for grep: grep$i=!$i||$b,map again -1 \$\endgroup\$
    – choroba
    Feb 18, 2016 at 17:22
3
\$\begingroup\$

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);}}
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – W.K.S
    Feb 17, 2016 at 7:48
  • \$\begingroup\$ 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 \$\endgroup\$
    – Erwan
    Feb 17, 2016 at 9:03
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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>

\$\endgroup\$
2
\$\begingroup\$

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/))}
\$\endgroup\$
2
\$\begingroup\$

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
>
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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());
            }
        }
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Rather than hard-coding your input, is it not possible to read it from the terminal or the command line or as a function parameter? \$\endgroup\$
    – Neil
    Feb 17, 2016 at 0:52
2
\$\begingroup\$

MATL, 31 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Esteemator
    Feb 17, 2016 at 14:10
  • \$\begingroup\$ @Esteemator Sorry, my mistake. Corrected \$\endgroup\$
    – Luis Mendo
    Feb 17, 2016 at 14:52
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ How about c>0 for c==1 and c<1 for c==0? Saves two bytes. \$\endgroup\$ Feb 19, 2016 at 6:39
  • \$\begingroup\$ Fantastic, we got it under 100! Thank you Michael. \$\endgroup\$ Feb 19, 2016 at 6:48
  • 1
    \$\begingroup\$ You'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'] \$\endgroup\$ Feb 19, 2016 at 7:21
  • 1
    \$\begingroup\$ That'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 (like x+y). Others start with ['1'..'9'] and [0..71] and then retain only the i`mod`9<8 results 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'] \$\endgroup\$ Feb 19, 2016 at 9:28
  • \$\begingroup\$ Ah, well that's still a good bit better \$\endgroup\$ Feb 19, 2016 at 9:54
1
\$\begingroup\$

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}&

\$\endgroup\$
1
\$\begingroup\$

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])
\$\endgroup\$
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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);}}}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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