18
\$\begingroup\$

Task: Have your program randomly choose one of the following two quotes at runtime and print that one quote, exactly as shown.

1:

Under the spreading chestnut tree
I sold you and you sold me.
There lie they, and here lie we
Under the spreading chestnut tree.

2:

WAR IS PEACE.
FREEDOM IS SLAVERY.
IGNORANCE IS STRENGTH.

Restrictions: The words spreading, chestnut, PEACE, FREEDOM, and STRENGTH must appear in your code. No word from the above two quotes may appear in your code more than once.

Shortest solution wins.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Ok I choosed the first. Honest! xkcd.com/221 \$\endgroup\$
    – edc65
    Commented Aug 2, 2014 at 8:48
  • 9
    \$\begingroup\$ @edc65 That's no longer funny. \$\endgroup\$ Commented Aug 2, 2014 at 9:40
  • 2
    \$\begingroup\$ @MartinBüttner not to be funny, but to suggest a more explicit rule. Reading first time I really tought I could choose a random one and then start to work. \$\endgroup\$
    – edc65
    Commented Aug 2, 2014 at 10:06
  • \$\begingroup\$ @edc65 Ah I see, the wording used to be different. \$\endgroup\$ Commented Aug 2, 2014 at 10:07

13 Answers 13

4
\$\begingroup\$

Perl (191 184 characters)

$_=$$&1?"0
I12321me6T4they,34we
06":"WAR5PEACE6FREEDOM5SLAVERY6IGNORANCE5STRENGTH6";s/\d/("Under the spreading chestnut tree"," sold ",you," and ","here lie "," IS ",".
")[$&]/ge;print

Thanks @core1024 for tips on golfing this.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can replace 6 with actual new lines, drop the quotes around "you" and use $$&1 as a random generator :) \$\endgroup\$
    – core1024
    Commented Aug 5, 2014 at 13:27
3
\$\begingroup\$

PHP 179

Inspired by es1024's answer.

<?=strtr(rand()%2?"0
I 1 232 1 me.
T5 they,35 we
0.
":"WAR4PEACE.
FREEDOM4SLAVERY.
IGNORANCE4STRENGTH.
",["Under the spreading chestnut tree",sold,you," and "," IS ","here lie"]);
\$\endgroup\$
3
\$\begingroup\$

Java, 338

I can't get it below 338...

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}

Bit ungolfed:

class B{public static void main(String[]a){String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";
if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);
else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Yes you can codegolf.stackexchange.com/questions/6671/…. \$\endgroup\$
    – Fabinout
    Commented Aug 5, 2014 at 14:29
  • \$\begingroup\$ Thanks for the link! Gonna search for something useful tomorrow. \$\endgroup\$
    – CoolWilly
    Commented Aug 5, 2014 at 14:40
  • \$\begingroup\$ @Fabinout I've tried a few tricks, but they didn't help. (java.io.PrintStream p=System.out, added 15 bytes to total, static<T>void p(T o){System.out.print(o);}, added 12). Any other suggestions? \$\endgroup\$
    – CoolWilly
    Commented Aug 6, 2014 at 7:11
  • \$\begingroup\$ use static block instead of a main declaration \$\endgroup\$
    – Fabinout
    Commented Aug 6, 2014 at 7:56
  • \$\begingroup\$ Static block *310 CHAR *: class B{static{String b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=".\n",i=" IS ";if(Math.random()>.5)System.out.print(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+h);else System.out.print("WAR"+i+"PEACE"+h+"FREEDOM"+i+"SLAVERY"+h+"IGNORANCE"+i+"STRENGTH"+h);}} \$\endgroup\$
    – Fabinout
    Commented Aug 6, 2014 at 11:36
2
\$\begingroup\$

Javascript 229 254 256

Edit 1 using new Date as suggested by @Doorknob - still unclear to me how javascript Dates morph to strings or integers or whatever

Edit 2 simplified. A lot.

t=(new Date&1
   ?'0WAR0 IS 0PEACE0.\n0FREEDOM020SLAVERY040IGNORANCE020STRENGTH.'
   :'0Under the spreading chestnut tree0\nI 0sold 0you 0and 04030me.\nT0here lie 0they, 05090we\n010.'
  ).split(0);
for(i=o='';j=t[++i];)o+=t[j|0||i];alert(o)
\$\endgroup\$
4
2
\$\begingroup\$

C# (268)(260) (256)

This is executable in LINQPad, using the Dump() method:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";if(new Random().Next(9)>4)(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();else("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.").Dump();

Ungolfed:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

if(new Random().Next(9)>4)
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".").Dump();
else
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.").Dump();

Update:

Using the ternary operator and 1 extra variable, I was able to cut another 6 characters:

string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";a=new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.");a.Dump();

Ungolfed:

string a,b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";

a=new Random().Next(9)>4 ?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+".") :
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.");

a.Dump();

Update2:

Thanks to the ingenious suggestion of tsavinho I was able to save 4 more chars by placing braces around the ternary operation:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";(new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")).Dump();

Ungolfed:

string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";
(new Random().Next(9)>4?
    (b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):
    ("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")
).Dump();
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You could save 4 more characters by removing a and replacing it with brackets like this: string b="Under the spreading chestnut tree",c="sold ",d="you ",e="and ",f="me.\nT",g="here lie ",h=" IS ";(new Random().Next(9)>4?(b+"\nI "+c+d+e+d+c+f+g+"they, "+e+g+"we\n"+b+"."):("WAR"+h+"PEACE.\nFREEDOM"+h+"SLAVERY.\nIGNORANCE"+h+"STRENGTH.")).Dump(); \$\endgroup\$
    – tsavinho
    Commented Aug 6, 2014 at 14:51
1
\$\begingroup\$

Python 3 - 228

for x in[b"AB:87078@624>049BA6",b"?;<61;=63;56"][id(id)%3-1]:print("and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")[x-48],end="")

Slightly ungolfed:

# id(id) returns an even number based on memory address of id
# id(id)%3-1 gives -1, 0 or 1
randomNumber = id(id)%3-1

# Word list
words = "and |FREEDOM|T|IGNORANCE|here lie |STRENGTH|.\n|you |sold |we|I | IS |PEACE|SLAVERY|they, |WAR|me|Under the spreading chestnut tree|\n".split("|")

# Byte Literals for Under the chestnut and WAR IS PEACE
# each byte correspond to the word's index in the word list
byteLiterals = [b"AB:87078@624>049BA6", b"?;<61;=63;56"]
choice = byteLiterals[randomNumber]

for x in choice: 
    print(words[x-48], end="") # Print each word out
\$\endgroup\$
1
\$\begingroup\$

PowerShell 205

("{0}.`nI{1}{2}{3}{2}{1} me.`nT{4}they,{3} {4}we`n{0}."-f"Under the spreading chestnut tree"," sold"," you"," and","here lie "),("WAR","PEACE.`nFREEDOM","SLAVERY.`nIGNORANCE","STRENGTH."-join" IS ")|Random

Uses the -f operator to put strings on the first quote, and the second one is joined by IS, after that it selects a random element of those 2 quotes and prints it...

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

JavaScript / jQuery 396

var B={
U:"Under the spreading chestnut tree",
s:"sold ",
y:"you ",
l:"lie ",
a:"and ",
i:"IS ",
b:"</br>"
};
var A={
P:B.U+B.b+"I "+B.s+B.y+B.a+B.y+B.s+"me."+B.b
+"There "+B.l+"they, "+B.a+"here "+B.l+"we."+B.b+B.U,
Q:"WAR "+B.i+"PEACE."+B.b+"FREEDOM "+B.i+"SLAVERY."+B.b
+"IGNORANCE "+B.i+"STRENGTH."
};
$(function(){
var z=(Math.random()<0.5)?A.P:A.Q; 
$('#d').append(z);
});
\$\endgroup\$
1
\$\begingroup\$

T-SQL, 337 327

Just for fun, I've made another solution in T-SQL, which is one byte shorter than my Java 8 solution:

DECLARE @b char(33)='Under the spreading chestnut tree',@c char(5)='sold',@d char(4)='you',@e char(4)='and',@f char(9)='here lie',@ char='
',@h char(4)=' IS'IF rand()>.5PRINT @b+@+'I '+@c+@d+@e+@d+@c+'me.'+@+'T'+@f+'they, '+@e+@f+'we'+@+@b+'.'ELSE PRINT'WAR'+@h+'PEACE.'+@+'FREEDOM'+@h+'SLAVERY.'+@+'IGNORANCE'+@h+'STRENGTH.'

By declaring the chars one too long, you can add an 'automatic' space, saving up a few bytes.

Using the code in the proposed edit by user PenutReaper, you can indeed shave off 10 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ Using a variable for carriage returns isn't worth it. Just use literal ones. \$\endgroup\$
    – Muqo
    Commented Sep 18, 2014 at 23:51
1
\$\begingroup\$

JavaScript, 233 bytes

Math.random()>0.5?(a="Under the spreading chestnut tree")+"\nI "+(s="sold")+(y=" you ")+(n="and")+y+s+" me.\nT"+(h="here ")+(l="lie ")+"they "+n+" "+h+l+"we\n"+a+".":["WAR","PEACE.\nFREEDOM","SLAVERY.\nIGNORANCE","STRENGTH."].join(" IS ")

Wrote this without looking at other answers. Makes good use of assignments as expressions.

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

05AB1E, 119 bytes

“‚  spreading chestnutíÍ““Iïê€î€ƒ€îïê€á.““€Çº¤€»,€ƒ€Îº¤€¦“).ªĆ»‘‡î€ˆ PEACE.
FREEDOM€ˆÃÒRY.
IGNORANCE€ˆ STRENGTH‘)'.«Ω

Try it online!

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

Python 3, 222 bytes

a,b,c,d,e,f='Under the spreading chestnut tree',' you ',' lie ','and','sold',' IS '
print({f'{a}\nI {e}{b}{d}{b}{e} me.\nThere{c}they, {d} here{c}we\n{a}.',f'WAR{f}PEACE.\nFREEDOM{f}SLAVERY.\nIGNORANCE{f}STRENGTH.'}.pop())

Try it online!

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

C (gcc), 307 289 271 270 bytes

-37 bytes thanks to ceilingcat

*y=" IS ";main(){int*r[]={"WAR",y,"PEACE.\nFREEDOM",y,"SLAVERY.\nIGNORANCE",y,"STRENGTH.",0,"Under the spreading chestnut tree","\nI"," sold ","you"," and ",r[11],r[10],"me.\n","T","here lie ","they,",r[12],r[17],"we\n",r[8],".",0},z=r;for(z=z/2&8;r[z];)printf(r[z++]);}

Try it online!

\$\endgroup\$
0

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.