160
\$\begingroup\$

Since Halloween is coming up I thought I might start a fun little code golf challenge!

The challenge is quite simple. You have to write a program that outputs either trick or treat.
"The twist?" you may ask. Well let me explain:

Your program has to do the following:

  • Be compilable/runnable in two different languages. Different versions of the same language don't count.
  • When you run the program in one language it should output trick and the other should output treat. The case is irrelevant and padding the string with whitespace characters are allowed (see examples).
  • This is , so the solution with the fewest bytes wins.

A few explanations:

Valid outputs (Just for the words not for running the code in the two languages. Also adding quotes to signalize the beginning or end of the output. Do not include them in your solution!):

"trick"

"Treat"

"    TReAt"

"
     tRICk          "

Invalid outputs:

"tri ck"

"tr
eat"

"trck"

I'm interested to see what you can come up with! Happy Golfing!

I'd like to note that this is my first challenge so if you have suggestions on this question please leave them in the form of a comment.

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=97472,OVERRIDE_USER=23417;function answersUrl(e){return"https://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"https://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=o.replace(TAGS_REG,"")),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,TAGS_REG = /(<([^>]+)>)/ig;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:400px;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\$
  • 21
    \$\begingroup\$ This meta answer states that near-duplicates can be tolerated if there's a good reason. I believe that the popularity this question receives from being close to Halloween is a good reason in itself, so I'll vote to reopen. I wouldn't mind closing it after Halloween (but I don't know if this would be a good thing either). \$\endgroup\$ – Aaron Oct 26 '16 at 14:57
  • 2
    \$\begingroup\$ @mbomb007. This is a duplicate of what? \$\endgroup\$ – TRiG Oct 27 '16 at 15:03
  • 3
    \$\begingroup\$ definitely not a duplicate. The only thing the same about that other one is that it's also a polyglot challenge with specified output. \$\endgroup\$ – Brian Minton Oct 28 '16 at 14:51
  • 3
    \$\begingroup\$ ... 3 pages... I really think that this is getting a lot of activity based on the current value of the seasonal variant. \$\endgroup\$ – wizzwizz4 Oct 29 '16 at 10:09
  • 2
    \$\begingroup\$ What a great question! I love how some of the answers illuminate and exploit how simple code fragments mean different things in different languages-- e.g. truthiness/falsiness and associativity of the ternary operator. \$\endgroup\$ – Don Hatch Nov 3 '16 at 22:28

126 Answers 126

4
\$\begingroup\$

Python 3 / JavaScript, 46 bytes

There weren't any "true" Python/JavaScript polyglot answers yet, so I figured I'd try my hand at writing one:

a=1//2;alert=print
alert(["trick","treat"][a])

Python 3

This is how Python sees the code:

a=1//2;alert=print
alert(["trick","treat"][a])

// is integer division in Python 3, so a=1//2; sets variable a to 0. Then alert=print sets the variable alert to the function print so that we can use alert to output.

In the next line, alert is called on the item at index a in the array ["trick","treat"]. Since a is 0 and alert is print, this prints "trick".

JavaScript

This is how JS sees the code:

a=1//2;alert=print
alert(["trick","treat"][a])

The first line is parsed as a=1; the rest is simply a comment. This sets variable a to 1.

The second line, much like in Python, alerts the item at index a in ["trick","treat"]. Since a is 1, this alerts "treat".

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

Perl / Ruby : 41 bytes

#line 9
print __LINE__==2?"treat":"trick"

Lines beginning with # are comments in both Ruby and Perl. Except that lines following (roughly) the regex #\s*line\s+\d+ are executed by Perl's compiler and change the line number as the compiler sees it (see the doc : Plain Old Comments (Not!)).

So when arriving to the second line, Perl thinks it the 9th while Ruby thinks it's the second. Hence Ruby will print treat while Perl will print trick.

\$\endgroup\$
  • \$\begingroup\$ I did not know that was possible! Good to know... Shame it's not global: perl -E'eval"#line 9\nsay __LINE__";say __LINE__' \$\endgroup\$ – Dom Hastings Nov 2 '16 at 9:16
  • \$\begingroup\$ @DomHastings I learned about it only a few days ago (I was bored so I went through the doc and found this :D ). Indeed, it would've been nice.. \$\endgroup\$ – Dada Nov 2 '16 at 9:39
4
\$\begingroup\$

PHP, Lua 41 Bytes

PHP print trick an Lua treat

--$a;echo"trick";/*
print("treat")
--*/
\$\endgroup\$
  • \$\begingroup\$ You don't need the 2nd $a;. That is inside comment in both languages. \$\endgroup\$ – manatwork Nov 1 '16 at 9:00
  • \$\begingroup\$ You can join the 2nd and 3rd lines. And better check your count as currently I see there only 39 bytes. \$\endgroup\$ – manatwork Nov 2 '16 at 15:19
4
\$\begingroup\$

JavaScript (ES6) / Japt, 47 33 22 bytes

1
alert`trick`;"treat"

JavaScript

This should be fairly obvious: 1 and "treat" do nothing, and alert`trick` alerts trick.

Japt

In Japt, lowercase letters (except those in strings) transpile to method calls, e.g. .a(. However, if it comes after another lowercase letter, it is instead transpiled to "a". All open parentheses are cut off at semicolons ;. This means that the code roughly transpiles to:

1 .a("l".e("r".t("trick")));"treat"

This is then evaluated as JavaScript. "r".t("trick") is "r", "l".e("r") is "l", and 1.a("l") is 1. However, the poor JS engine's work is pointless: only the result of the last expression is sent to STDOUT, so the 1 is discarded and treat is printed.

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

CJam / HTML, 17 14 bytes

Edit:

  • -3 bytes off. Thanks to @ETHProductions

Code:

TREAT<"trick"

HTML:

TREAT<;"trick"
TREAT           print TREAT
     <          tag open: ignore rest since there is no closing bracket

CJam:

TREAT<;"trick"
TREAT<         e#random stuff / push vars
      ;        e#discard stack
       "trick" e#literal trick
               e#implictly print
\$\endgroup\$
  • \$\begingroup\$ I think HTML is not considered a programming language. \$\endgroup\$ – FliiFe Oct 31 '16 at 9:45
  • \$\begingroup\$ I think you can remove the !--. \$\endgroup\$ – ETHproductions Nov 3 '16 at 21:55
  • \$\begingroup\$ @FliiFe Lets say it's PHP. Would work too. \$\endgroup\$ – Roman Gräf Nov 6 '16 at 12:43
  • \$\begingroup\$ @FliiFe you're allowed to use non-programming-languages in constant output challenges. \$\endgroup\$ – Pavel Apr 24 '17 at 1:58
4
\$\begingroup\$

////D1ffe7e45e, 59 bytes

See what I did there?

/@//TREAT
/37333120813633633333333363333336222222226//
/$/__/

Basically, in D1ffe7e45e / begins and ends comments. So, TREAT is in a comment but the source that prints TRICK isn't. The __ is two no-ops to stop the D1ffe7e45e program from repeating. The source that /// interprets should be self-explanatory.

\$\endgroup\$
  • \$\begingroup\$ Well those comments disappeared o_O \$\endgroup\$ – Timtech Oct 27 '16 at 23:24
  • \$\begingroup\$ I don't think this works again. the /@/__/ is changed to //__/, so it doesn't halt \$\endgroup\$ – Destructible Lemon Oct 27 '16 at 23:27
  • \$\begingroup\$ (replace the first or second @ with something else) \$\endgroup\$ – Destructible Lemon Oct 27 '16 at 23:28
  • \$\begingroup\$ Yeah makes sense, this is my first time with this language ^_^ \$\endgroup\$ – Timtech Oct 28 '16 at 11:06
4
\$\begingroup\$

Julia/Python, 31 Bytes

Nothing special, just taking advantage of the similarities between the languages. Julia uses 1 as the first index, while Python uses 0.

l=["trick","treat"]
print(l[1])

Try it online - Julia!

Try it online - Python!

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Unless Julia does something I don't know, you can remove the extra spaces in the first line: list=["trick","treat"] and you can also rename list l to save some more bytes \$\endgroup\$ – Stephen Aug 10 '17 at 21:05
  • \$\begingroup\$ Welcome to PPCG, nice first golf! And you still haven't removed the space after , as far as I can tell. \$\endgroup\$ – Zacharý Aug 10 '17 at 21:21
  • \$\begingroup\$ Forgot about that one! Thanks! \$\endgroup\$ – Rohan D Aug 10 '17 at 21:24
3
\$\begingroup\$

Scala / Javascript, 33 bytes

if(0==false)"trick"; else "treat"

0==false returns true in javascript, but false in scala

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

Befunge-98/DOS .COM file, 27 bytes

00000000  22 20 b4 09 ba 0b 01 cd  21 c3 20 74 72 65 61 74  |" ......!. treat|
00000010  24 6b 63 69 72 74 22 34  6b 2c 40                 |$kcirt"4k,@|
0000001b

The Befunge-98 part pushes the characters of a string to the stack, and then outputs the top 5 characters in the stack and ends.

The DOS part is:

00000000  2220              and ah,[bx+si]
00000002  B409              mov ah,0x9
00000004  BA0B01            mov dx,0x10b
00000007  CD21              int 0x21
00000009  C3                ret

Where the first instruction is just some dummy non important instruction, and the rest calls INT 21h function 09h, which prints a $-terminated string pointed to by DS:DX.

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

Charcoal and Pip, 13 bytes

trick©"treat"

Charcoal

Charcoal's code page reads this as trick»"treat". trick prints the string to the canvas. » closes a block; apparently, if it is unmatched, it ends the program. The canvas is then printed to the screen.

Try it online

Pip

t              Variable (no-op)
 r             Generate random number (and do nothing with it)
  i            Variable (no-op)
   c           Variable (no-op)
    k          Variable (no-op)
     ©         Unrecognized character, ignored
      "treat"  String, output implicitly

Try it online

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

C/Lua, 65 bytes

#include<stdio.h>/*
print"trick"--[[*/
main(){puts("treat");}//]]
\$\endgroup\$
3
\$\begingroup\$

Ruby / Brat, 26 bytes

#*
puts:treat
$*#p :trick

Ruby prints treat and Brat prints trick.

Ruby

The first line is a comment starting with #, so ignored. The second line prints out treat (the parser is liberal enough we don't need a space).

The last line is interpreted as $* (a global variable) and the rest is ignored as a comment starting with #.

Brat

In Brat, #* begins a multi-line comment and *# ends it.

The first line begins a multi-line comment, so the second line is ignored.

Brat sees $ as part of the comment and *# closes the comment started above. That leaves p :treat to print out treat.

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

Ruby / ECMA6 27 bytes

{'t':'trick'}['t']||'treat'

Turns out you don't need to assign a JSON object in oredr to use it in ecma6

Original, with plain Javascript:

Ruby/Javscript 32 31 bytes

a={'t':'trick'};a['t']||'treat'

Because ruby treats the key as as symbol (:t), does not match with 't' so goes to the or. Whereas JavaScript is not as fussy.

\$\endgroup\$
  • \$\begingroup\$ Cool trick! I'm not sure if this qualifies as a full program/function though :(. \$\endgroup\$ – m-chrzan Oct 27 '16 at 13:40
3
\$\begingroup\$

Python/Brainf**k (87 Bytes)

print("treat")#+++++++++[>+++++++++<-]>+++.--.<+++[>---<-]>.<++[>---<-]>.<++++[>++<-]>.

Python ignores everything after the # because its a comment and brainf**k ignores print("treat")# because they are unrecognized characters.

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

Forth/Perl, 53 bytes

1 ?dup : print"trick" ." treat" ; print"trick" or bye

Needs a case-insensitive Forth (e.g: GNU Forth).

When run in Forth, it defines a print"trick" word that outputs "treat", then calls it.

When run in Perl, it uses the colon of Forth's word definition as part of a conditional statement. In ?dup, the question mark forms part of the aforementioned conditional statement, and dup gets treated as a bareword (a string without quotes). The dot in Forth's ." primitive gets interpreted as Perl's concatenation operator, and bye gets treated as another bareword.

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

C / C++, 72 bytes

#include<stdio.h>
main(){char*a[]={"trick","treat"};puts(a[1//**/2
]);}

Compile with gcc/g++ -ansi and don't regard warnings. A C90-conforming C compiler does not recognize // as a comment but it skips /**/, leaving puts(a[1/2]);. C++ will see puts(a[1]);.

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

Bash / Wolfram Language (a.k.a. Mathematica), 19 bytes

echo trick;#&@treat

For Bash, # is a comment indicator so it just outputs trick. Mathematica multiplies two undefined symbols echo and trick but ignores the result and proceeds to the second command, which fully expanded goes

(Function[Slot[1]])[treat]

and evaluates to treat.

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

C / Javascript, 70 47 bytes

(excluding comments C=22 + Javascript=14 ~= 36 )

/**\
/main(){puts("trick");}
//*/alert("treat")

What C sees:

main(){puts("trick");}

What javascript sees:

alert("treat")

Explanation:

/* C comment ends with '/' on next line but js comment remains open  *\ 
/ //BEGIN C Block 
#define function int 
//*/alert("this whole line is commented in C, but // is in the js comment")

//for additional tricks...
/*Most compilers can build K&R style C with parameters like this:*/ 
function volume(x,y,z)/**\ 
/int x,y,z;/**/ 
{ 
  return x*y*z; 
} 

/**\ 
/ 
#undef function 
#define var const char** 
#define new (const char*[]) 
#define Array(...)  {__VA_ARGS__} 
/**/ 

var cars = new Array("Ford", "Chevy", "Dodge"); 

/* Or a more readable version *\ 
/// BEGIN C Block 
#undef var 
#undef new 
/* END C Block */ 
\$\endgroup\$
3
\$\begingroup\$

Windows Batch, JScript, 35 bytes

trick="treat";WScript. echo (trick)

The batch version displays the parentheses as delimiters.

\$\endgroup\$
  • \$\begingroup\$ This outputs (trick) instead of trick. \$\endgroup\$ – Ismael Miguel Oct 30 '16 at 5:24
  • \$\begingroup\$ yes, the batch version displays the parentheses as delimiters. \$\endgroup\$ – peter ferrie Nov 1 '16 at 19:50
  • \$\begingroup\$ Actually, batch displays whatever you write after. In this case, it will show (trick). If you wrote "trick", it would show that. \$\endgroup\$ – Ismael Miguel Nov 1 '16 at 23:32
  • \$\begingroup\$ but then it won't be valid JavaScript, which requires the parentheses to run the function. \$\endgroup\$ – peter ferrie Nov 2 '16 at 16:45
  • \$\begingroup\$ Can you use backticks? \$\endgroup\$ – Ismael Miguel Nov 2 '16 at 18:16
3
\$\begingroup\$

Ruby / Crystal, 31 bytes

puts 'a'=="a"&&"trick"||"treat"

'a' is a string literal in Ruby and a character literal in Crystal. "a" is a string literal in both. This abuses that difference to detect the language the script is ran in.

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

Pyth and SMBF, 28 bytes

"trick"<.q<.q<.q<.q<.q"taert

"trick" explanation:

Pyth has a quit-type operation with .q, which I use at <.q to end the program. Everything afterwards is uninterpreted, but in order for me to fix arity I need to add arity 0 data after each arity-2 <. Otherwise, the program requires user input (although what Python datum the input wouldn't matter, since it's not used). Pyth will implicitly print the "trick" at the beginning of the program.

"treat" explanation:

The only characters that SMBF uses are as follows (the others are no-ops or not examined in memory): "<.<.<.<.<.taert. The <. sets print out the last five characters of the program in reverse order (hence treat is reversed into taert to have it print as treat).

\$\endgroup\$
  • 2
    \$\begingroup\$ I appreciate the game you've set up, but can you remove the spoiler from the title please? :) \$\endgroup\$ – Beta Decay Oct 28 '16 at 6:09
3
\$\begingroup\$

Actually and Python 2, 21 bytes

thanks to quartata for this solution

print"trick"#X"treat"

Try it online: Actually, Python 2

Explanations

Actually:

print"trick"#X"treat"
print                  5 NOPs (they only do things if the stack isn't empty)
     "trick"           push "trick"
            #          listify
             X         discard (stack is now empty)
              "treat"  push "treat"
                       (implicitly print)

Python 2:

print"trick"#X"treat"
print"trick"           # print "trick"
            #X"treat"  # a comment
\$\endgroup\$
  • 1
    \$\begingroup\$ print"trick"#X"treat" is shorter but much more boring. \$\endgroup\$ – a spaghetto Oct 31 '16 at 20:45
3
\$\begingroup\$

Windows Batch, VBScript, 34 bytes

trick="treat":WScript._
echo trick

The Batch echo will display the immediate text.
The VBScript uses the "_" line-continuation to run the "echo" command, which displays the contents of the variable "trick", which contains the string "treat".
VBScript does not require parenthesis for functions which do not return a value.

\$\endgroup\$
  • \$\begingroup\$ Can you explain how this works ? \$\endgroup\$ – FliiFe Oct 31 '16 at 9:42
  • \$\begingroup\$ @FliiFe, updated my answer. \$\endgroup\$ – peter ferrie Nov 1 '16 at 19:55
3
\$\begingroup\$

HTML / JavaScript (ES6), 28 bytes

trick<alert`treat`;var trick

Test HTML | Test JS

Explanation

HTML is a lot like ///: it writes out any plain text, ignoring anything after a < until it gets to a >. Since there are no >s, it just writes "trick".

JS is simple: alert`treat`. However, to avoid a ReferenceError for trick not being defined, we have to add a var trick to tell JS that trick is really supposed to be there.

I wanted to add CSS that "prints" or, but it seems to be impossible:

  • If the CSS is before the <, it gets written to the HTML document.
  • If the CSS is after the <, it finds a syntax error and stops before running.
\$\endgroup\$
3
\$\begingroup\$

Haskell / Ruby, 74 bytes

--0;True=false
main=if True then print "trick" else print "treat";--0;end

Haskell

Anything starting with -- is treated as a comment. Actual program in Haskell is now just

main=if True then print "trick" else print "treat";

which prints "trick".

Ruby

--0 is just -(-0). So the first line declares a constant True with a value false. Next line does the actual printing and since True is false, it prints "treat". main is just treated as a variable in ruby and is assigned the value 0.

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! \$\endgroup\$ – DJMcMayhem Nov 3 '16 at 16:14
3
\$\begingroup\$

sh / csh, 32 bytes

echo trick #treat|sed 's/.*#//'

For csh, the command must be executed interactively.

Explanation: In interactive mode, sh treats anything starting with # as a comment. csh does so only in batch mode.

ksh and bash behave the same way as sh. tcsh behaves like csh. Some of these shells have options to alter this behavior.

zsh behaves like csh for this particular command.

\$\endgroup\$
  • 1
    \$\begingroup\$ bash has interactive_comments shopt` and zsh has INTERACTIVE_COMMENTS to control this behaviour. \$\endgroup\$ – ninjalj Nov 3 '16 at 1:28
3
\$\begingroup\$

C / C++, 73 bytes

#include<stdio.h>
int main(){puts("treat\0trick"+sizeof'1'/sizeof 1*6);}

This could fail (printing trick in both C and C++) on an implementation with sizeof (int) == 1, which implies CHAR_BIT >= 16. As far as I know there are no such implementations in the real world.

Changing int main() to int main(void) would make it theoretically more conforming in C, but in practice all C and C++ compilers accept int main().

\$\endgroup\$
  • 1
    \$\begingroup\$ We don't really require portability here; as long as it works in one implementation that predates the challenge, it's valid. With gcc/g++ and the right OS/architecture, you can replace include with import, remove int and replace sizeof 1*6 with 2*3. \$\endgroup\$ – Dennis Nov 3 '16 at 20:34
3
\$\begingroup\$

Labyrinth/Perl, 38 bytes

As cat -v:

$_=116.114.105.99.107.$@^"^@^@^L^B^_";print

Where ^@ represents ASCII 0 (␀), ^B represents ASCII 2 (␂), ^L represents ASCII 12 (␌), and ^_ represents ASCII 31 (␟). Or, as a hexdump -C:

00000000  24 5f 3d 31 31 36 2e 31  31 34 2e 31 30 35 2e 39  |$_=116.114.105.9|
00000010  39 2e 31 30 37 2e 24 40  5e 22 00 00 0c 02 1f 22  |9.107.$@^"....."|
00000020  3b 70 72 69 6e 74                                 |;print|
00000026

I have wanted to post a Labyrinth/Perl answer since reading Emigna's Labyrinth/><> answer. In that answer, Labyrinth code looks very much like it starts with a Perl version string.

So, in this answer, Labyrinth just does the following:

$_=                    → dummy operations
116.114.105.99.107.    → print string
$                      → dummy op
@                      → end

while Perl does:

$_=                    → set the $_ variable to:
 116.114.105.99.107    →  a version string for the other language
 .$@                   →  concatenated with $EVAL_ERROR (null when no eval error)
 ^"^@^@^L^B^_";        →  XORed with "\0\0\x0c\2\x1F", to get "treat" from "trick"
print                  → and finally print the string

Labyrinth/Perl alternate version, 105 bytes

Here is another version, modifying the string in Labyrinth code, instead of in Perl code:

; $_=116.114.105.99.107.$@
; ;print;      1+ 7+ 16
; ;;;;;;;;;;;;; 1
;              ;;
;;;;;9^_9^10^00 ^a

Labyrinth code follows the path. The last line modifies the code, rotating columns using ^. After the last line has been interpreted, the field looks like:

; $_=116.114.101.97.116.$@
; ;     ;       +  +      
; ;;;;;;;;;;;;;;1         
;               ;         
;;;;;9^_9^10^005^ 9  07   

After finishing interpreting the last line, Labyrinth follows the path upwards. Then, the 1 makes it attempt to turn rightwards, but hitting the wall makes it turn leftwards instead. Afterwards, Labyrinth just follows the path, eventually reaching code already explained for the first approach (where Perl modifies the string).

As you'll notice, (in 9^_9^10^00 ^ 1) numbers in Perl can have _ in them, to make them more readable.


Labyrinth/Perl, linear labyrinth version, 76 bytes

Not as fun as the previous one, but does the trick (or treat, as it may be):

25^25^25^23^0;$;=116.114.105.99.107.$@
        ; print$    ;   ;  1+ 7+ 16
;

Labyrinth/Perl, boring version, 49 bytes

Separate code paths for Labyrinth and Perl:

;print 116.114.105.99.107
;
116.114.101.97.116.$@
\$\endgroup\$
3
\$\begingroup\$

HTML/Foo, 19 15 12 bytes

trick<"treat

Displays trick in HTML and prints treat in Foo.

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

Bash / Batch, 37 32 bytes

The perfect contrast, Linux vs. Windows!

echo;echo trick;exit<0echo treat

How it works on Linux: Try it online!

echo;                               -echo nothing
     echo trick;exit;               -echo trick
                     <0echo treat   -ignored by parser (already exited)

How it works on Windows:

echo;                               -echo nothing
                    <0              -find directory (with argument of 0)                  
     echo trick;exit                -can't find directory (skips command)
                      echo treat    -echo treat

-1 byte, Thanks to CartManager XD

-4 bytes by golfing a better way to comment

\$\endgroup\$
  • \$\begingroup\$ Why do you need the comment # if you have exited? I don't know batch but I would think echo;echo trick;exit;>NUL&echo treat would work \$\endgroup\$ – Sriotchilism O'Zaic Apr 23 '17 at 5:05
  • \$\begingroup\$ Or even echo;echo trick;#>NUL&echo treat. \$\endgroup\$ – Sriotchilism O'Zaic Apr 23 '17 at 5:09
  • \$\begingroup\$ @CartManagerXD, that's a good idea, but for some reason the space in between exit and # has to be there. Although, you're right, you can remove the #, and then replace the space with a ;. All in all it removes one byte \$\endgroup\$ – Graviton Apr 23 '17 at 22:54
  • \$\begingroup\$ What is the purpose of the first echo? \$\endgroup\$ – Cows quack Aug 10 '17 at 19:19
  • \$\begingroup\$ @Cowsquack Without it, windows will also echo trick. For some reason echoing something before the echo tick renders everything up to the <0 unparsable for windows. \$\endgroup\$ – Graviton Aug 11 '17 at 1:35

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.