# Determine the color of a chess square

Your challenge is to write a program that outputs the color of a given square from the chessboard. This is how a chessboard looks:

You can see that the square a1 is dark, and h1 is a light square. Your program needs to output dark or light, when given a square. Some examples:

STDIN:  b1
STDOUT: light

STDIN:  g6
STDOUT: light

STDIN:  d4
STDOUT: dark

The rules:

• You need to provide a full program that uses STDIN and uses STDOUT to output dark or light.
• Assume that the input is always valid ([a-h][1-8])
• This is , so shortest amount of bytes wins!

# Scoreboard

• Why hasn't anyone tried <>^Fish? Nov 14 '15 at 16:34

# Python 2, 41 38 bytes

print'ldiagrhkt'[int(input(),35)%2::2]

3 bytes thanks to Mego for string interlacing

Takes input like "g6". That's light and dark intertwined.

• That's just gorgeous with the string interlacing. Nov 16 '15 at 20:08
• I'd actually say that int(input(),35) is the brilliant part. I thought of the string interlacing, but your input method saves the most bytes. Nov 17 '15 at 1:29
• +1 if you un-golf and explain your answer, please May 24 '20 at 18:55

# GS2, 17 15 bytes

de♦dark•light♠5

The source code uses the CP437 encoding. Try it online!

### Verification

$xxd -r -ps <<< 6465046461726b076c696768740635 > chess.gs2$ wc -c chess.gs2
15 chess.gs2
q}
cdark

s/[1357aceg]//g removes all odd-indexed coordinates. The resulting pattern buffer then has length of 1 for "light" or length of 0 or 2 for "dark". /^.$/ matches the 1-length patterns, changes the pattern to "light" and quits. Otherwise the pattern is changed to "dark". # Pyth, 18 bytes @c2"lightdark"iz35 Interpret the input as a base 35 number, chop lightdark in half, print. ## Python 2, 45 bytes print'dlairgkh t'[sum(map(ord,input()))%2::2] Takes input like "a1". Try it online • This wouldn't work in Python 3 due to the lack of parens for the print. Nov 13 '15 at 22:29 • Can't test right now but something like "ldiagrhgt"[expression::2] should work while saving a byte or two Nov 13 '15 at 22:47 # ShadyAsFuck, 91 bytes / BrainFuck, 181 bytes My first real BrainFuck program, thank Mego for the help and for pointing me to the algorithm archive. (That means I didn't really do it on my own, but copied some existing algorithms. Still an experience=) NKnmWs3mzhe5aAh=heLLp5uR3WPPPPagPPPPsuYnRsuYgGWRzPPPPlMlk_PPPPPP4LS5uBYR2MkPPPPPPPP_MMMkLG] This is of course the translation from my brainfuck answers: ,>,[<+>-]++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[-]++++++++++[>++++++++++<-]<[<+>>+<-]<[>+<-]+>>[>++++++++.---.--.+.++++++++++++.<<<->>[-]]<<[>>>.---.+++++++++++++++++.-------.<<<-] Developed using this interpreter/debugger. I stole two code snippets for divmod and if/else from here. (Thanks to @Mego!) ,>, read input [<+>-] add ++< set second cell to 2 Now we have the cells config >sum 2 we now perform the divmod algorithm: [->-[>+>>]>[+[-<+>]>+>>]<<<<<]> [-]> The output of the divmod looks like this 0 d-n%d >n%d n/d but we zeroed the d-n%d and are zeroing the next cell too: >[-] Fill one cell up to the value 100 for easier outputting: ++++++++++[>++++++++++<-]< Now the configuration is >cond 0 100 and for applying the if/else algorithm we need two temp variables, so we choose the configuration temp0 >c temp1 100 c[<temp0+>>temp1+<c-]<temp0[>c+<temp0-]+ >>temp1[ #>++++++++.---.--.+.++++++++++++.< outputs light <<temp0- >>temp1[-]] <<temp0[ #>>>.---.+++++++++++++++++.-------.<<< outputs dark temp0-] ## Seriously, 19 bytes "dark""light"2,O+%I Takes input like "a1" Try it online (you will have to manually enter the input; the permalinks don't like quotes) • Online link ded.. Apr 2 '16 at 21:57 # Turing Machine Code, 235 bytes Using the rule table syntax defined here. 0 a _ r 1 0 c _ r 1 0 e _ r 1 0 g _ r 1 0 * _ r 2 1 2 _ r 3 1 4 _ r 3 1 6 _ r 3 1 8 _ r 3 2 1 _ r 3 2 3 _ r 3 2 5 _ r 3 2 7 _ r 3 * * _ r 4 3 _ l r A A _ i r B B _ g r C C _ h r D D _ t r halt 4 _ d r E E _ a r F F _ r r G G _ k r halt • This is possibly the most amazing thing I have ever seen lol Nov 29 '15 at 23:09 # TI-BASIC, 66 bytes Tested on a TI-84+ calculator. Input Str1 "light If inString("bdfh",sub(Str1,1,1)) xor fPart(.5expr(sub(Str1,2,1 "dark Ans Here's a more interesting variation on the third line, which sadly is exactly the same size: Input Str1 "dark If variance(not(seq(inString("bdfh2468",sub(Str1,X,1)),X,1,2 "light Ans You'd think TI-BASIC would be decent at this challenge, since it involves modulo 2. It's not; these solutions seem to be the shortest possible. We spend a lot of bytes to get both characters in the string, but what really costs is the thirteen two-byte lowercase letters. # JavaScript (ES6), 45 bytes alert(parseInt(prompt(),35)%2?"dark":"light") • way to go to use a radix! +1 FTW... Oct 5 '17 at 20:59 • Is there a particular reason alert and prompt have to be the way that STDOUT and STDIN are interpreted for JavaScript? If a normal arrow function is allowed, then it would just be: x=>parseInt(x,35)%2?"dark":"light" May 24 '20 at 11:46 • @SteveBennett the challenge asks for a full program so a function wouldn't be allowed as that requires separate code to execute it. May 25 '20 at 18:27 • But your code isn't a "full program" either because it would need to be embedded within an HTML page running in a browser. Seems like kind of a dodgy requirement. May 26 '20 at 0:58 ## Befunge-93, 393733 31 bytes All credit to Linus who suggested this 31-byte solution: <>:#,_@ v%2-~~ "^"light"_"krad Test it using this interpreter. ### Explanation < v%2-~~ The < at the beginning sends the instruction pointer to the left, where it wraps around to the right. It then reads in two characters from input as ASCII, subtracts them, and does a modulo by 2. As a and 1 are both odd (in terms of ASCII code), this works. The v redirects the instruction pointer downward... "^"light"_"krad ...onto the _, which sends the instruction pointer to the left if the top of stack is 0 and to the right otherwise. The characters of "light" or "dark", respectively, are pushed onto the stack in reverse order. Both paths hit the ^ at the left, which sends the instruction pointer upward... >:#,_@ ...to the output segment. : duplicates the top of stack, # jumps over the , and onto the _, which sends the instruction pointer to the right if the top of stack is 0 and left otherwise. When the stack is empty, the top of stack (after :) is 0, so the instruction pointer hits the @ which stops execution. Otherwise, it hits the ,, which outputs the top of stack as a character, and then the # jumps it over the : and onto the >, which starts the process again. • save a byte using rad"v>"k without a space? Nov 15 '15 at 2:43 • @Linus: "The space is necessary because otherwise the output would be dar k." Try it in the linked online interpreter. Nov 15 '15 at 2:49 • Your right. Anyway, I was going to do this in befunge but I can only get 2 bytes under you... <>:#,_@ v%2-~~\n"^"light"_"krad, fix the newline. Nov 15 '15 at 3:29 • @Linus: That's brilliant. Thanks! Nov 15 '15 at 3:38 • @JamesHolderness, No hard feelings. You're right to point out this doesn't work in the original Befunge-93 interpreter, the actual spec is for an 80x25 torus. You might want to post your version as it's own answer and explain the difference. I think at least that would be more practical than debating year-old hobby code with me. Feb 13 '17 at 19:12 # C, 55 bytes s;main(){puts(strtol(gets(&s),0,19)&1?"light":"dark");} Try it online Thanks DigitalTrauma for lots of golfing tips • I think you have an extra ( after puts Nov 13 '15 at 23:41 • This for 55: s;main(){puts(strtol(gets(&s),0,19)&1?"light":"dark");}. Assumes that the integer width is big enough to hold 3 chars of string. You should also be able to do main(s){puts(strtol(gets(&s),0,19)&1?"light":"dark");} for 54, though for some reason gets() is returning garbage is s if not global, so it segfaults. Nov 14 '15 at 2:32 • oh wow, base-19. clever. Nov 15 '15 at 7:41 # C, 49 bytes main(c){gets(&c);puts(c+c/256&1?"light":"dark");} • No, that doesn't compile. – xsot Nov 16 '15 at 1:45 • Oh, my bad, I had fiddled with something else. The output is wrong, though. I think you meant to do gets(&c)%256+c/256? – Lynn Nov 16 '15 at 1:51 • Oh, good catch. Though at this point, my solution is strictly worse than yours as we're using the same technique. Looks like I have plenty to learn. – xsot Nov 16 '15 at 1:59 • It turns out that the wrong output was caused by the return value of gets(&c). I have updated my submission accordingly. – xsot Nov 16 '15 at 2:16 # Japt, 23 22 bytes Japt is a shortened version of JavaScript. Interpreter Un19 %2?"dark":"light" ### How it works // Implicit: U = input string Un19 // Convert U from a base 19 number to decimal. %2 // Take its modulo by 2. ?"dark" // If this is 1, return "dark". :"light" // Else, return "light". // Implicit: output last expression Using the new version 0.1.3 (released Nov 22), this becomes 17 bytes, shorter than all but GS2: Un19 %2?»rk:¦ght Or, alternatively, a magic formula: (26 bytes) Un19 %2*22189769+437108 sH Un19 %2 // Convert input to base 19 and modulo by 2. *22189769+437108 // Where the magic happens (top secret) sH // Convert to a base 32 string. # Java, 157127 124 bytes interface L{static void main(String[]a){System.out.print(new java.util.Scanner(System.in).nextInt(35)%2>0?"dark":"light");}} • You could use an interface like this : interface i{static void main since the everything in an interface is public by default Dec 4 '15 at 10:49 # TeaScript, 23 bytes ®x,35)%2?"dark":"light" Unfortunately the strings dark and light can't be compressed. • Hehe, Japt is shorter for once ;) +1 though, the JS compression techniques are great! I may add them into Japt after revamping the interpreter. Nov 13 '15 at 23:57 # Ruby, striked out 44 36 bytes puts %w[light dark][gets.to_i(19)%2] • You can save a byte by replacing puts with$><< (no space).
– Lynn
Nov 15 '15 at 15:48
• @Mauris I know, but i like my terminating newline Nov 18 '15 at 10:00
• You can save 3 bytes by changing puts for p Apr 2 '16 at 21:13

# BotEngine, 165 14x11=154

v acegbdfh
>ISSSSSSSS
v<<<<>v<<P
vS1   vS2ke
vS3   vS4re
vS5   vS6ae
vS7   vS8de
>     >   ^
>     >  v
^S2   ^S1el
^S4   ^S3ei
^S6  P^S5eg
^S8 te^S7eh
^   <

Here it is with the different path segments highlighted:

(Any non-space characters not highlighted serve as arguments for the e and S instructions- each of these instructions uses the symbol to the left (relative to the bot's direction of travel) as its argument)

# 𝔼𝕊𝕄𝕚𝕟, 26 chars / 34 bytes

ô(שǀ(ï,ḣ)%2?dark:light”

Try it here (Firefox only).

• I wouldn't call it "compression" if it takes more bytes :P Nov 14 '15 at 2:55
• I'm more worried about chars than bytes at this point. I've entirely given up on trying to golf down byte count in 𝔼𝕊𝕄𝕚𝕟... Nov 14 '15 at 2:56
• We always score by bytes, and while it's often interesting to optimize for a secondary objective, remember that the fewest bytes always wins. Nov 14 '15 at 3:50
• Yeah, I understand that. I'm not really aiming for winning as much though. Nov 14 '15 at 4:05

## Clojure, 63 bytes

(pr (['light 'dark] (mod (Integer/parseInt (read-line) 35) 2)))
• We read in a line from stdin with (read-line)
• Then parse the string into an integer value in base 35 using a call to a JVM method
• Taking mod of the result 2 tells us if it is even or odd
• Use the result returned from the modulo function as an index to the sequence and print it

I save a worthy 2 bytes by quoting out "light" and "dark" with a single quote so that Clojure takes it as a literal, as opposed to wrapping each word in a pair of quotation marks. I also save a few bytes by using pr rather than println.

Some info on quoting in Clojure

• Welcome to Programming Puzzles and Code Golf! This is a nice first answer. :) I'm not too familiar with Clojure; would you mind adding an explanation? Nov 17 '15 at 5:30
• Absolutely! There you go. Let me know if you have any questions!
– dmh
Nov 17 '15 at 5:45

# C, 46 bytes

main(c){gets(&c);puts(c%37%2?"light":"dark");}

Expects an environment where ints are stored little-endian, and are at least two bytes.

## Explanation

c is argc, so initially it contains 01 00 00 00. gets will read two chars, say a (0x61) and 1 (0x31), and store them in c, which is now

61 31 00 00

representing the number 0x3161, or 12641.

Essentially, in this problem, given c = x + 256*y, we want to compute (x + y) mod 2, and print a string accordingly. To do this, I could have written c % 255 % 2, as then

(x + 256 * y) % 255 % 2
= (x % 255 + y % 255) % 2      since 256 ≡ 1 (mod 255)
= (x + y) % 2                  since 0 < x, y < 255

However, 37 also works:

(x + 256 * y) % 37 % 2
= (x % 37 - 3 * (y % 37)) % 2  since 256 ≡ -3 (mod 37)

x is in the range 49-57 inclusive (digits 1-8), so x % 37 == x - 37.

y is in the range 97-104 inclusive (lowercase a-h), so y % 37 == y - 74.

This means we can simplify to

= (x - 3 * y + 185) % 2
= (x + y + 1) % 2              since -3 ≡ 185 ≡ 1 (mod 2)

and simply flip the strings to correct for the parity.

# Beam, 127 bytes

rSr>+v
^  )
n()nS<
>L'''''>+++++)S>+++)@---@'''>+++++)++@-------@H
>L'''''>+++)S>++++++)+++@---@--@+@'''>++++)@H

An explanation Light blue - read a character from input into beam, save the beam value into the store, read a character from input into beam.

Dark blue - Adds store to beam by decrementing store to 0 while incrementing the beam

Light green - An even odd testing construct. The loop will exit to the left if the beam is even or the right if odd.

Dark green - Outputs dark

Tan - Outputs light

# O, 22 17 bytes

i#2%"light'dark"?

This does what it is required to do, with no additional benefits.

# Matlab, 51 bytes

I do not think this needs any explanation=)

a={'light','dark'};disp(a(2-mod(sum(input('')),2)))

## Brian & Chuck, 66 bytes

,>,_{->-?+{-_?>}<?light{-_?>}>>?dark?
II{<?}<<<?{<{<<<?_>.>.>.>.>.

Probably still golfable, but I think I'd need another approach.

### Explanation

,>, reads input into Chuck, replacing the two Is. Next is the following part of the code:

_{->-?
{<?

which decrements both input elements until the latter (i.e. the digit) reaches zero. This stops Brian's ? from passing control to Chuck, continuing on.

The next + increments the zeroed digit to a 1 so that following uses of { don't get caught on it. At this point, the first cell of Chuck's tape has the difference of the two code points, so now we need to take the code point modulo 2. This is done with the following parts:

A            B            C
{-_?>}<?     {-_?>}>>?    ?
}<<<?{<{<<<?

I've labelled the three parts on Brian's tape to make things easier to explain. The {- in parts A and B decrement the first cell on Chuck's tape, and the following ? checks if it's zero. If it's not, then control is passed, and we execute }<<<?. For part A, this moves us to part B. For part B, this moves us to part C, which immediately passes control and we execute {<{<<<?, sending us back to part A. Thus the effect is that we alternate between parts A and B, in a state machine-like way.

Now whether the first cell was zeroed while we were in part A or part B determines what we print. For A, we have:

?>}<?light
_               ?_>.>.>.>.>.

which executes >}< to position us on the last ? in Chuck's tape, and then runs >. five times to print "light".

On the other hand, for part B, we have:

?>}>>?dark?
_                _>.>.>.>.>.

which executes >}>> to position us on the first . in Chuck's tape, and then runs >. four times to print "dark".

## Brainfuck, 132 bytes

>,>,[<->-]<[->+<[->-]<[<]>]<++++++++++[->++++++++++<]>>[<++++++++.---.--.+.++++++++++++.>->+<]>-[<<.---.+++++++++++++++++.-------.<]

I tried coming up with my own mod 2 algorithm, which is the [->+<[->-]<[<]>].

# Batch, 248223 207 bytes

Because Batch lacks disjunctional conditionals. -18 bytes thanks to @dohaqatar7, and more due to his idea.

@ECHO OFF
SET S=SET
%S%/P I=
%S%L=%I:~0,1%
%S%I=IF %L%==
%S%N=%I:~-1%
%S%A= %S%L=
%I%a%A%0
%I%b%A%1
%I%c%A%0
%I%d%A%1
%I%e%A%0
%I%f%A%1
%I%g%A%0
%I%h%A%1
%S%/A R=N%%2
IF %R%%L% (ECHO light) ELSE (ECHO dark)
• I'm not on a windows machine so I can't test right now but, you should be able to save some bytes by abusing variable expansion. Put set a=" set L=" at the top then replace every occurrence of  set L= with %a%. Nov 16 '15 at 23:09
• @dohaqatar7 Oh right you can do that. Thanks! Nov 16 '15 at 23:18
• when SET is used with switches (/A or P) spaces are ignored, like %S%/P I= can be shortened into %S%/PI=. Furthermore, parenthesis around ECHO dark isn't required Mar 30 '20 at 10:22