58
\$\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\$
  • 1
    \$\begingroup\$ Why hasn't anyone tried <>^Fish? \$\endgroup\$ – ghosts_in_the_code Nov 14 '15 at 16:34

77 Answers 77

46
\$\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\$
  • 8
    \$\begingroup\$ That's amazing! With 9 unavoidable bytes, 3 byte outgolfing Pyth and CJam is amazing. \$\endgroup\$ – isaacg Nov 13 '15 at 23:30
  • 29
    \$\begingroup\$ Holy cow, guys, GS2 is the new Pyth! Somebody figure it out how to use it well before Denni...never mind. \$\endgroup\$ – ETHproductions Nov 14 '15 at 5:16
56
\$\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\$
  • \$\begingroup\$ That's just gorgeous with the string interlacing. \$\endgroup\$ – Wayne Werner Nov 16 '15 at 20:08
  • 5
    \$\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 '15 at 1:29
26
\$\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
    \$\begingroup\$ Ooh, pretty colors! \$\endgroup\$ – Celeo Nov 13 '15 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 '15 at 0:53
  • 18
    \$\begingroup\$ I really don't get all these golf languages. \$\endgroup\$ – juniorRubyist Nov 14 '15 at 6:35
  • 4
    \$\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\$ – Martin Ender Nov 14 '15 at 10:03
  • 3
    \$\begingroup\$ @qwr I thought being contrived was the point of esolangs. ;) \$\endgroup\$ – Martin Ender Nov 15 '15 at 10:45
21
\$\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\$
  • 34
    \$\begingroup\$ your code is smiling :-) \$\endgroup\$ – Doorknob Nov 13 '15 at 22:23
  • 8
    \$\begingroup\$ I did consider the equally effective :^) \$\endgroup\$ – Peter Taylor Nov 14 '15 at 7:29
  • 2
    \$\begingroup\$ Please can you explain how this works. \$\endgroup\$ – Fogmeister Nov 14 '15 at 11:16
  • \$\begingroup\$ @Fogmeister, added explanation. \$\endgroup\$ – Peter Taylor Nov 14 '15 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\$
14
\$\begingroup\$

Pyth, 18 bytes

@c2"lightdark"iz35

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

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

Python 2, 45 bytes

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

Takes input like "a1". Try it online

\$\endgroup\$
  • \$\begingroup\$ This wouldn't work in Python 3 due to the lack of parens for the print. \$\endgroup\$ – isaacg Nov 13 '15 at 22:29
  • \$\begingroup\$ Can't test right now but something like "ldiagrhgt"[expression::2] should work while saving a byte or two \$\endgroup\$ – FryAmTheEggman Nov 13 '15 at 22:47
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\$
  • 2
    \$\begingroup\$ Online link ded.. \$\endgroup\$ – CalculatorFeline Apr 2 '16 at 21:57
10
\$\begingroup\$

JavaScript (ES6), 45 bytes

alert(parseInt(prompt(),35)%2?"dark":"light")
\$\endgroup\$
  • \$\begingroup\$ way to go to use a radix! +1 FTW... \$\endgroup\$ – WallyWest Oct 5 '17 at 20:59
9
\$\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\$
  • \$\begingroup\$ This is possibly the most amazing thing I have ever seen lol \$\endgroup\$ – Lucas Nov 29 '15 at 23:09
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\$
9
\$\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\$
  • \$\begingroup\$ save a byte using rad"v>"k without a space? \$\endgroup\$ – Linus Nov 15 '15 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\$ – El'endia Starman Nov 15 '15 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 '15 at 3:29
  • \$\begingroup\$ @Linus: That's brilliant. Thanks! \$\endgroup\$ – El'endia Starman Nov 15 '15 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 '17 at 19:12
8
\$\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\$
8
\$\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\$
  • \$\begingroup\$ You could use an interface like this : interface i{static void main since the everything in an interface is public by default \$\endgroup\$ – Yassin Hajaj Dec 4 '15 at 10:49
7
\$\begingroup\$

TeaScript, 23 bytes

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

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

\$\endgroup\$
  • \$\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\$ – ETHproductions Nov 13 '15 at 23:57
7
\$\begingroup\$

Ruby, striked out 44 36 bytes

puts %w[light dark][gets.to_i(19)%2]
\$\endgroup\$
  • \$\begingroup\$ You can save a byte by replacing puts with $><< (no space). \$\endgroup\$ – Lynn Nov 15 '15 at 15:48
  • \$\begingroup\$ @Mauris I know, but i like my terminating newline \$\endgroup\$ – daniero Nov 18 '15 at 10:00
  • \$\begingroup\$ You can save 3 bytes by changing puts for p \$\endgroup\$ – Cyoce Apr 2 '16 at 21:13
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\$
  • \$\begingroup\$ I think you have an extra ( after puts \$\endgroup\$ – Level River St Nov 13 '15 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\$ – Digital Trauma Nov 14 '15 at 2:32
  • \$\begingroup\$ oh wow, base-19. clever. \$\endgroup\$ – fluffy Nov 15 '15 at 7:41
7
\$\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\$
7
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 26 chars / 34 bytes

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

Try it here (Firefox only).

\$\endgroup\$
  • 1
    \$\begingroup\$ I wouldn't call it "compression" if it takes more bytes :P \$\endgroup\$ – lirtosiast Nov 14 '15 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\$ – Mama Fun Roll Nov 14 '15 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 '15 at 3:50
  • \$\begingroup\$ Yeah, I understand that. I'm not really aiming for winning as much though. \$\endgroup\$ – Mama Fun Roll Nov 14 '15 at 4:05
7
\$\begingroup\$

C, 49 bytes

main(c){gets(&c);puts(c+c/256&1?"light":"dark");}
\$\endgroup\$
  • \$\begingroup\$ No, that doesn't compile. \$\endgroup\$ – xsot Nov 16 '15 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 '15 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 '15 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 '15 at 2:16
7
\$\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\$
  • \$\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 '15 at 5:30
  • \$\begingroup\$ Absolutely! There you go. Let me know if you have any questions! \$\endgroup\$ – MONODA43 Nov 17 '15 at 5:45
5
\$\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\$
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\$
5
\$\begingroup\$

Labyrinth, 48 46 45 42 bytes

Thanks to Sp3000 for saving two bytes.

-,"
#
%0:::8.5.3.4.116.@
1
00.97.114.107.@

Try it online!

Explanation

The beginning of the code is a funny dead end. Remember that Labyrinth assumes an infinite number of zeroes when it requires operands at the bottom of the stack. The code starts one the - going right, which tries to subtract two numbers, so the stack becomes:

[ ... 0 ]

Then , reads the first character, a say:

[ ... 0 97 ]

The " is a no-op, but this is also a dead-end so the instruction pointer turns around and starts going to the left. Then ` reads the other character, 2 say:

[ ... 0 97 50 ]

This time, - subtracts those two numbers:

[ ... 0 47 ]

The IP now follows the bend of the "corridor". The # gets the stack depth, ignoring the implicit zeroes, which conveniently happens to be 2:

[ ... 0 47 2 ]

And % computes the modulo:

[ ... 0 1 ]

At this point, the IP is at a junction. If the top of the stack is zero, it will move straight ahead, where 100.97.114.107.@ prints dark. But if the top of the stack is non-zero (specifically, 1), it will move to the right, where 0:::8.5.3.4.116.@ prints light (note that we can omit the leading 1, because there is already a 1 on the stack, and we can save on the repeated 10 in 108, 105, 103, 104 by making a few copies of the 10 when we first get there).

\$\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\$

><>, 31 bytes

ii+2%?\"krad"oooo;
l"oc0.\"thgi

Here I'm thinking "there's got to be a better way..."

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

Perl, 29 27 bytes

$_=/./&($'+ord)?light:dark

This code requires the -p switch, which I have counted as 1 byte.

Try it online on Ideone.

How it works

  • Because of the -p switch, Perl reads one line of input and stores it in $_.

  • /./ is a regular expression that matches one character. This has two implications:

    • Since the match is successful, /./ returns 1.

    • The post-match (second input character) is stored in $'.

  • $'+ord adds the integer the second input character represents to the code point (ord) of the first character of the implicit variable $_.

  • & takes the bitwise AND of the return value of /./ and the sum $'+ord, returning 1 is the sum if odd, 0 if it is even.

  • ?light:dark returns light if the previous expression returned 1 and dark otherwise.

  • Finally $_= assigns the result to $_, which Perl prints automatically, because of the -p switch.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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