72
\$\begingroup\$

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

enter image description here

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

var QUESTION_ID=63772,OVERRIDE_USER=8478;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why hasn't anyone tried <>^Fish? \$\endgroup\$ Nov 14, 2015 at 16:34
  • \$\begingroup\$ shortest amount of bytes total or in a given language? \$\endgroup\$ Jan 6, 2023 at 22:31

100 Answers 100

64
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ That's just gorgeous with the string interlacing. \$\endgroup\$ Nov 16, 2015 at 20:08
  • 8
    \$\begingroup\$ 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. \$\endgroup\$
    – mbomb007
    Nov 17, 2015 at 1:29
  • 4
    \$\begingroup\$ +1 if you un-golf and explain your answer, please \$\endgroup\$
    – Aryan
    May 24, 2020 at 18:55
48
\$\begingroup\$

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
$ gs2 chess.gs2 <<< b1
light

How it works

d               Add the code points of the input characters.
 e              Compute the sum's parity.
  ♦             Begin a string literal.
   dark
       •        String separator.
        light
             ♠  End the string literal; push as an array of strings.
              5 Select the element that corresponds to the parity.
\$\endgroup\$
3
  • 9
    \$\begingroup\$ That's amazing! With 9 unavoidable bytes, 3 byte outgolfing Pyth and CJam is amazing. \$\endgroup\$
    – isaacg
    Nov 13, 2015 at 23:30
  • 34
    \$\begingroup\$ Holy cow, guys, GS2 is the new Pyth! Somebody figure it out how to use it well before Denni...never mind. \$\endgroup\$ Nov 14, 2015 at 5:16
  • \$\begingroup\$ Did you just steal GS2's powerful string literal system to Jelly? \$\endgroup\$
    – user92069
    Jun 7, 2020 at 4:40
25
\$\begingroup\$

Hexagony, 34 32 bytes

,},";h;g;;d/;k;-'2{=%<i;\@;trl;a

Unfolded and with annotated execution paths:

enter image description here
Diagram generated with Timwi's amazing HexagonyColorer.

The purple path is the initial path which reads two characters, computes their difference and takes it modulo 2. The < then acts as a branch, where the dark grey path (result 1) prints dark and light grey path (result 0) prints light.

As for how I compute the difference and modulo, here is a diagram of the memory grid (with values taken for the input a1):

enter image description here
Diagram generated with Timwi's even more amazing Esoteric IDE (which has a visual debugger for Hexagony).

The memory pointer starts on the edge labelled row, where we read the character. } moves to the edge labelled col, where we read the digit. " moves to the edge labelled diff where - computes the difference of the two. ' moves to the unlabelled cell where we put the 2, and {= moves to the cell labelled mod where we compute the modulo with %.

This might be golfable by a few bytes by reusing some of the ;, but I doubt it can be golfed by much, certainly not down to side-length 3.

\$\endgroup\$
7
  • 7
    \$\begingroup\$ Ooh, pretty colors! \$\endgroup\$
    – Celeo
    Nov 13, 2015 at 23:39
  • 1
    \$\begingroup\$ This language is new to me but I am amazed at your ability to come up with something more contrived than I thought possible \$\endgroup\$
    – qwr
    Nov 14, 2015 at 0:53
  • 19
    \$\begingroup\$ I really don't get all these golf languages. \$\endgroup\$
    – Joseph
    Nov 14, 2015 at 6:35
  • 5
    \$\begingroup\$ @codeSwift4Life Hexagony is far from being a golfing language. For trivial tasks like this it might be reasonably competitive, because it has single-character commands, but that is more a necessity shared by many other 2D languages, including Befunge, Piet, ><>. Any nontrivial task will require very large amounts of code and complicated programs, due to Hexagony's weird memory model. It is in no way meant to be a concise language, but rather an exotic and weird one, exploring programming on hexagonal grids. \$\endgroup\$ Nov 14, 2015 at 10:03
  • 4
    \$\begingroup\$ @qwr I thought being contrived was the point of esolangs. ;) \$\endgroup\$ Nov 15, 2015 at 10:45
20
\$\begingroup\$

CJam, 18 bytes

r:-)"lightdark"5/=

Online demo

Dissection

r               e# Read a token of input
:-              e# Fold -, giving the difference between the two codepoints
)               e# Increment, changing the parity so that a1 is odd
"lightdark"5/   e# Split the string to get an array ["light" "dark"]
=               e# Index with wrapping, so even => "light" and odd => "dark"
\$\endgroup\$
4
  • 36
    \$\begingroup\$ your code is smiling :-) \$\endgroup\$
    – Doorknob
    Nov 13, 2015 at 22:23
  • 10
    \$\begingroup\$ I did consider the equally effective :^) \$\endgroup\$ Nov 14, 2015 at 7:29
  • 2
    \$\begingroup\$ Please can you explain how this works. \$\endgroup\$
    – Fogmeister
    Nov 14, 2015 at 11:16
  • \$\begingroup\$ @Fogmeister, added explanation. \$\endgroup\$ Nov 14, 2015 at 17:52
17
\$\begingroup\$

sed, 37

s/[1357aceg]//g
/^.$/{clight
q}
cdark

Explanation

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".

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

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

Python 2, 45 bytes

print'dlairgkh t'[sum(map(ord,input()))%2::2]

Takes input like "a1". Try it online

\$\endgroup\$
2
  • \$\begingroup\$ This wouldn't work in Python 3 due to the lack of parens for the print. \$\endgroup\$
    – isaacg
    Nov 13, 2015 at 22:29
  • \$\begingroup\$ Can't test right now but something like "ldiagrhgt"[expression::2] should work while saving a byte or two \$\endgroup\$ Nov 13, 2015 at 22:47
12
\$\begingroup\$

Pyth, 18 bytes

@c2"lightdark"iz35

Interpret the input as a base 35 number, chop lightdark in half, print.

\$\endgroup\$
12
\$\begingroup\$

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)

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Online link ded.. \$\endgroup\$ Apr 2, 2016 at 21:57
10
\$\begingroup\$

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is possibly the most amazing thing I have ever seen lol \$\endgroup\$
    – Lucas
    Nov 29, 2015 at 23:09
10
\$\begingroup\$

JavaScript (ES6), 45 bytes

alert(parseInt(prompt(),35)%2?"dark":"light")
\$\endgroup\$
4
  • \$\begingroup\$ way to go to use a radix! +1 FTW... \$\endgroup\$ Oct 5, 2017 at 20:59
  • \$\begingroup\$ 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" \$\endgroup\$ May 24, 2020 at 11:46
  • \$\begingroup\$ @SteveBennett the challenge asks for a full program so a function wouldn't be allowed as that requires separate code to execute it. \$\endgroup\$
    – Downgoat
    May 25, 2020 at 18:27
  • \$\begingroup\$ 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. \$\endgroup\$ May 26, 2020 at 0:58
9
\$\begingroup\$

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.

\$\endgroup\$
8
\$\begingroup\$

Befunge-93, 39 37 33 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.

\$\endgroup\$
5
  • \$\begingroup\$ save a byte using rad"v>"k without a space? \$\endgroup\$
    – Linus
    Nov 15, 2015 at 2:43
  • \$\begingroup\$ @Linus: "The space is necessary because otherwise the output would be dar k." Try it in the linked online interpreter. \$\endgroup\$ Nov 15, 2015 at 2:49
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Linus
    Nov 15, 2015 at 3:29
  • \$\begingroup\$ @Linus: That's brilliant. Thanks! \$\endgroup\$ Nov 15, 2015 at 3:38
  • \$\begingroup\$ @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. \$\endgroup\$
    – Linus
    Feb 13, 2017 at 19:12
7
\$\begingroup\$

C, 55 bytes

s;main(){puts(strtol(gets(&s),0,19)&1?"light":"dark");}

Try it online

Thanks DigitalTrauma for lots of golfing tips

\$\endgroup\$
3
  • \$\begingroup\$ I think you have an extra ( after puts \$\endgroup\$ Nov 13, 2015 at 23:41
  • \$\begingroup\$ 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. \$\endgroup\$ Nov 14, 2015 at 2:32
  • \$\begingroup\$ oh wow, base-19. clever. \$\endgroup\$
    – fluffy
    Nov 15, 2015 at 7:41
7
\$\begingroup\$

C, 49 bytes

main(c){gets(&c);puts(c+c/256&1?"light":"dark");}
\$\endgroup\$
4
  • \$\begingroup\$ No, that doesn't compile. \$\endgroup\$
    – xsot
    Nov 16, 2015 at 1:45
  • \$\begingroup\$ 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? \$\endgroup\$
    – Lynn
    Nov 16, 2015 at 1:51
  • \$\begingroup\$ 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. \$\endgroup\$
    – xsot
    Nov 16, 2015 at 1:59
  • \$\begingroup\$ It turns out that the wrong output was caused by the return value of gets(&c). I have updated my submission accordingly. \$\endgroup\$
    – xsot
    Nov 16, 2015 at 2:16
7
\$\begingroup\$

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

Java, 157 127 124 bytes

interface L{static void main(String[]a){System.out.print(new java.util.Scanner(System.in).nextInt(35)%2>0?"dark":"light");}}
\$\endgroup\$
1
  • \$\begingroup\$ You could use an interface like this : interface i{static void main since the everything in an interface is public by default \$\endgroup\$ Dec 4, 2015 at 10:49
6
\$\begingroup\$

TeaScript, 23 bytes

®x,35)%2?"dark":"light"

Unfortunately the strings dark and light can't be compressed.

\$\endgroup\$
1
  • \$\begingroup\$ Hehe, Japt is shorter for once ;) +1 though, the JS compression techniques are great! I may add them into Japt after revamping the interpreter. \$\endgroup\$ Nov 13, 2015 at 23:57
6
\$\begingroup\$

Ruby, striked out 44 36 bytes

puts %w[light dark][gets.to_i(19)%2]
\$\endgroup\$
3
  • \$\begingroup\$ You can save a byte by replacing puts with $><< (no space). \$\endgroup\$
    – Lynn
    Nov 15, 2015 at 15:48
  • \$\begingroup\$ @Mauris I know, but i like my terminating newline \$\endgroup\$
    – daniero
    Nov 18, 2015 at 10:00
  • \$\begingroup\$ You can save 3 bytes by changing puts for p \$\endgroup\$
    – Cyoce
    Apr 2, 2016 at 21:13
6
\$\begingroup\$

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:

enter image description here

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

\$\endgroup\$
6
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 26 chars / 34 bytes

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

Try it here (Firefox only).

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

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

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – Alex A.
    Nov 17, 2015 at 5:30
  • \$\begingroup\$ Absolutely! There you go. Let me know if you have any questions! \$\endgroup\$
    – dmh
    Nov 17, 2015 at 5:45
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Beam, 127 bytes

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

An explanation enter image description here 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

\$\endgroup\$
5
\$\begingroup\$

O, 22 17 bytes

i#2%"light'dark"?

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

\$\endgroup\$
4
\$\begingroup\$

Matlab, 51 bytes

I do not think this needs any explanation=)

a={'light','dark'};disp(a(2-mod(sum(input('')),2)))
\$\endgroup\$
4
\$\begingroup\$

Minkolang 0.12, 28 24 bytes

on+2%t"dark"t"light"t$O.

Try it here.

Explanation

o                   Take character from input
n                   Take integer from input
+                   Add
2%                  Modulo by 2
t      t       t    Ternary; runs first half if top of stack is 0, second half otherwise
 "dark" "light"     Pushes the string "dark" or "light", depending.
$O.                 Output the whole stack as characters and stop.
\$\endgroup\$
4
\$\begingroup\$

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".

\$\endgroup\$
4
\$\begingroup\$

Brainfuck, 132 bytes

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

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

\$\endgroup\$
0
4
\$\begingroup\$

Batch, 248 223 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)
\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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%. \$\endgroup\$ Nov 16, 2015 at 23:09
  • \$\begingroup\$ @dohaqatar7 Oh right you can do that. Thanks! \$\endgroup\$ Nov 16, 2015 at 23:18
  • \$\begingroup\$ 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 \$\endgroup\$
    – ScriptKidd
    Mar 30, 2020 at 10:22

Your Answer

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

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