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

2
\$\begingroup\$

Lua, 53 Bytes

l,n=(...):byte(1,2)print(l%2==n%2 and"dark"or"light")

Pretty simple, takes command line input through ... and assigns variables l and n to the first and second byte of the input and then checks the ASCII value of each. If both are even or both are odd, the square is dark, else the square is light.

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

><> Fish 26 bytes

d"darkthgil"ii+2%?!rooooo;

A spin on the already posted fish code by sp3000.

It uses the same based checking with mod but with a few changes.

Using 1 line allows us to save 6 bytes (1 for the new line, 2 for directional instructions and 3 for the jump instructions)

Lose 1 byte to placing a [CR] onto the stack but it allows us to use 5 prints on both answers.

Lose 1 byte to reversing the stack [r] when needed for an answer.

Lastly putting both answers on the stack in 1 string allows us to save 2 bytes not having to use ["] twice.

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

SmileBASIC, 66 bytes

INPUT L$N=INSTR(@bdfh,L$)<0!=VAL(POP(L$))MOD 2?"light"*N;"dark"*!N

explanation:

INSTR(@bdfh,L$)<0 'Checks if L$ is b,d,f,h. @bdfh is a label, equivalent to the string "@bdfh"
!= 'used as a logical XOR, which SB doesn't have.
VAL(POP(L$))MOD 2 'Checks if the row is odd. Also removes the second character of L$, 
                   'which makes the first check shorter since SB evaluates right to left.
?"light"*N;"dark"*!N 'this turned out to be shorter than using IF/THEN/ELSE.
\$\endgroup\$
2
\$\begingroup\$

C# (Visual C# Interactive Compiler) - 122 42

Write((Read()+Read())%2<1?"dark":"light");
  • Reads from STDIN
  • Format is simply a1

Thanks to @dana for the change from C# to the current one & golfing a ton of bytes! :)

Try It Online!

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

AHK, 78 bytes

a=%1%
If Mod(Asc(a)+Asc(SubStr(a,2)),2)=1
s=light
Else
s=dark
FileAppend,%s%,*

AHK uses 1 as the name for the first parameter so you have to assign to a different name before you use it in functions. Otherwise, it'll think you mean the value 1 and not the variable named 1. Also, the only way to report to STDOUT is by using FileAppend with * as the file name.

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

K (oK), 16 bytes

`dark`light 2!+/

Try it online!

+/ sum the (code points of the) argument

2! mod-2

darklight  select that from this list of symbols

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

Jelly, 11 bytes

OḂEị“_ß“ṗɠ»

Try it online!

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

Chip, 57 bytes

*gS!~s
fA.Z\ZZZvt
cZ}x< eab
>--^/vZZvZZvt
ZZZd ddac ce
ab

Try it online!

XOR's the low bit of each input (like most if not all other answers) to make the decision. Outputs dabh`, bitwise-OR'd with hhe`t for light or ``pc for dark. (Dark also exits one byte early).

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

Japt, 16 bytes

`ä•Krk`qe g~Uxc

Try it online!

How it works

`ä•Krk`qe g~Uxc

`ä•Krk`          Compressed literal for "lightedark"
        qe        Split with "e"
           g      Take the element at the index (wrapping)...
             U      Input array of chars
              xc    Sum the chars' charcodes
            ~       Take bitwise not, in order to swap parity

The string array compression trick did quite a job here.

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

05AB1E (legacy), 13 bytes

“–°‡Ž“#I35öÈè

Try it online!

Here is a golf with your own language :)

Explanation

“–°‡Ž“       : compressed: "dark light"
      #      : split by space
            è: select the 0th or 1st element based on {
       I35ö  :  take the input and convert it to int from base 35
           È :  1 if even else 0
               }
\$\endgroup\$
0
\$\begingroup\$

Java, 178 bytes

interface z{static void main(String[]a){int b=a[0].charAt(0)-'a',c=a[0].charAt(1)-'0';if(b%2==0&&c%2!=0||b%2!=0&&c%2==0)System.out.print("dark");else System.out.print("light");}}

Takes input as first argument.

Ungolfed:

interface z {
    static void main(String[]a) {
        int b = a[0].charAt(0) - 'a',                               // Convert value from a to value from 0...7
                c = a[0].charAt(1) - '0';                           // Get number
        if (b % 2 == 0 && c % 2 != 0 ||                             // If b is divisible by 2 and c is divisible by 2 or
                b % 2 != 0 && c % 2 == 0)                           // b is not divisible and c is divisible
            System.out.print("dark");                               // output dark
        else System.out.print("light");                             // else output light
    }
}
\$\endgroup\$
0
\$\begingroup\$

Tcl, 63 bytes

scan [gets stdin] %c%d h v
puts [expr ($h+$v)%2?"light":"dark"]

Try it online!

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

QBIC, 38 bytes

~(asc(;)+!_sA,-1|!)%2|?@Light`\?@Dark

Explanation

~(                       IF
  asc( )                 the ascii value of
      ;                  the input string (of the input "b1" only the first char is evaluated by asc())
        + _s ,  |        plus a substring of
            A            A$ (the input, this was assigned to A$ by ;)
              -1         taking only the first char from the right
         !        !      and cast this to num
                   )%2   MODULO 2 (is non-zero, implicit)
|?@Light`                THEN PRINT the literal "Light"
\?@Dark                  ELSE PRINT "Dark" (no terminating ` is needed at EOF)
\$\endgroup\$
0
\$\begingroup\$

F# (Mono), 63 bytes

printf(if(stdin.Read()+stdin.Read())%2<1 then"dark"else"light")

Try it online!

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

APL (Dyalog Unicode), 25 bytesSBCS

Requires ⎕IO←0 (zero-indexing).

'dark' 'light'⊃⍨2|+/⎕UCS⍞

Try it online!

 get text from stdin

⎕UCS convert to Universal Character Set code points

+/ sum

2| 2-mod

'dark' 'light'⊃⍨ use that to pick from the list of strings

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

Kotlin, 45 bytes

{if(it.sumBy{it-'@'}%2>0)"light" else "dark"}

Try it online!

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

Runic Enchantments, 25 bytes

iu+2%1(8*?"light"@"dark"@

Try it online!

How it works

>                      implicit entry
 i                     read string from input
  u                    break the string into characters
   +                   add them together
    2%                 modulo 2
      1(               compare with 1
        8*             multiply the result (true -> 8, false -> 0)
          ?            pop x, if x is truthy, skip x characters
           "light"@    output light if false
           "dark"@     ouput dark if true
\$\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.