10
\$\begingroup\$

The goal of a challenge is to write solutions in as many languages as possible. Show off your programming multilingualism!

In this challenge, you will create a program that will generate spam messages, given an input seed-text. For example, given the input

[Thank you/Wow/Great], this [blog/post/writing] [gives/provides/delivers] [great/excellent/wonderful/superior] [content/information/data]. You [may/could] [find/get/retrieve] [similar/related] [content/information/data] at spammysite.com.

A possible output could be

Thank you, this writing delivers excellent data. You may find similar content at spammysite.com.

(idea from https://security.stackexchange.com/q/37436/29804)

There's a catch, though! You will also have to output a program in a different language. This program, when run, will also output the spam text and a program in a different language, which will also output the spam text and a program in a different language...

Your score will be character count / (number of languages ^ 2).

For example, I could have a Ruby program. It would take the input seed-text as input, and output the random spam text and a Python program.

Running this Python program (with no input), it would output another random spam text and a Perl program.

Running this Perl program (no input), it would output another random spam text.

In this example, my score would be the length of the original Ruby program divided by 9 (I have 3 languages, and 3 ^ 2 is 9).

Other miscellaneous things:

  • You must use at least 2 different programming languages.
  • Please list each program that is output. This way if someone doesn't have all of the languages, they can still see their sources. You may use this short example text: [Please/Kindly] [visit/see] my [site/blog]., or you may just use a placeholder (like {insert text here}).
  • You may assume that there will be no non-alphanumeric characters except []/.,, to simplify some things.
  • Different versions of languages do not count as different languages (ex. Python 2 == Python 3).
  • It must be fully random, i.e. not predictable and outputting a different combination every run. It must also be able to output every combination in an arbitrary amount of runs.
  • Since this is as well as , the shortest overall score (charcount / (langs ^ 2)) will win.
\$\endgroup\$
2
  • \$\begingroup\$ is it okay if the distribution is not uniform, e.g if the first word gets chosen more often than the other? \$\endgroup\$
    – mniip
    Commented Jan 20, 2014 at 12:12
  • \$\begingroup\$ @mniip Hmm, I never really specified that :-P I suppose you could do that and bend the rules if it saves characters \$\endgroup\$
    – Doorknob
    Commented Jan 20, 2014 at 15:02

2 Answers 2

5
\$\begingroup\$

PHP 5.5 -> Ruby 2.0 -> Python 2.7 -> Lua -> C# -> Java -> PdfTeX -> GolfScript 855 / 8^2 = 13.359375

The main ideas were:

  1. Instead of actually creating the logic to create the n+1th language in all programming languages do this only one time, and only use the basic random and stdout writing facilities of each programming language
  2. To shorten the template generator, just compress it with zLib. Make this generator the second language
  3. Use PHP as the first language, as it has built-in zLib decompressor

So the initial code is:

<?php $x=file_get_contents("php://stdin");echo preg_replace_callback("/\[(.*?)\]/",function($m){$r=explode("/",$m[1]);return $r[array_rand($r)];},$x).gzuncompress("BINARY_DATA")."__END__\n".$x;

Where BINARY_DATA is the gzcompressed version of the template generator (e.g. the result of the php code, which is the ruby version)

The complete initial code in base64 (should be 855 bytes after decoding):

PD9waHAgJHg9ZmlsZV9nZXRfY29udGVudHMoInBocDovL3N0ZGluIik7ZWNobyBw
cmVnX3JlcGxhY2VfY2FsbGJhY2soIi9cWyguKj8pXF0vIixmdW5jdGlvbigkbSl7
JHI9ZXhwbG9kZSgiLyIsJG1bMV0pO3JldHVybiAkclthcnJheV9yYW5kKCRyKV07
fSwkeCkuZ3p1bmNvbXByZXNzKCJ42o2UwXKbMBCG7zyFZj0doElV55BLiNJmmump
7aHpTA+Yg4xErA6SGCQSZ4jevZIBx3biTHywzO6y/7+fYRm5uf5zjVtOGS5XWjbY
NLWwSZzHKVx4OTa06Z/oE51iRZw6XFzV1FquXiQ/+2TEMKflKsSbViiLfFx4OTbK
puYuajpr0OUlBDmIoqrVErVUMX8I2ejWXCIvL0oejVfm0USznk0y4Fx4YWaxsUx3
Fj+0wvJkKE9mPcVCmYaX1qUpuI+wUODlgvzwjeI4XHg5NtSu8CBnOGeJNthcbsmT
NN0ToWSnHbatx1DEp3Hv/GyZ0KMyzXfaJWen4Z6aqzu7cmmRZvseknQ8c0JyT2FZ
ixKVNTUGUdRPAWOp9ce9Fgz9pEIlqc/dPhrLJf49UGqJ4g9oL1x4OTak2a79Jfl0
JXs5eiHk7AuM5d+0Mrrm+O/G/8zXVKI19hlcXAYXICqUtPgXX9sb7V15OJdzfJ72
R3uYlah2ezheG97DyTKXxQk4D1wiW+YUs64pJiRceGFm9fohFA+THM19hTd5ycDr
1nrId3mBaHtnArx/9J7izop6n99h+N0Iw4M3/JELeAlwATsIlUf4vdbUHhA8bLHP
L7R4P8FtXHhhZloNI6wJUMjeGGS0fIFgMX5EpTp5Pp9fTYGGVZ0SlW4l4/eCWn42
n8/R1qqb6oJLNLmcgpXwbtdkjU1XXHg5Ntn0RlxcvULQW17AYnsj4xWafuvG+lEc
6mf9dnD3IVQHvgjgDQxceDAwLyi8audAq3e+MR3g+O7gXHgwMJ4fxgPMR1TZ8zLc
yiDIw17YJFx4OTZPEPuBXFwMG4FiZ2GEJUJceDAw+dHiEB1cXIx1x4zAdoTlI98U
uciNR1QQ4ndQ5HdeFFZt9B9+DwFHIikuIl9fRU5EX19cbiIuJHg7

Let's go through all of the languages with the test input string [Thank you/Wow/Great], this is [awesome/wonderful].

PHP

$ cat ti2 | php a.php
Great, this is wonderful.
d=DATA.read.chomp.split('[').map{|a|a.split(']')}.flatten.map{|a|a.split('/')}
d.each{|a|print a.sample}
puts <<"DATA"

from random import choice
import sys
#{d.map{|a|"sys.stdout.write(choice(#{a.inspect}))"}*"\n"}
print
print '''math.randomseed(os.time())
#{d.map{|a|"a=#{a.inspect.tr('[]','{}')};io.write(a[math.random(1,#{a.length})]);"}*"\n"}
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
#{d.map{|a|b=->m{m.length==1?"System.Console.Write(#{m.first.inspect});":"if (r.NextDouble()<0.5){System.Console.Write(#{m.shift.inspect});}else{"+b[m]+"};"};b[a.dup]}*"\n"}
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
#{d.map{|a|b=->m{m.length==1?"System.out.print(\"#{m.first.inspect}\");":"if (r.nextFloat()<0.5){System.out.print(\"#{m.shift.inspect}\");}else{"+b[m]+"};"};b[a.dup]}*"\n"}
System.out.println();
#{x="a";d.map{|a|b=->m{m.length==1?m.first: "\\\\\\\\ifnum500>\\\\\\\\pdfuniformdeviate1000 #{m.shift}\\\\\\\\else "+b[m]+"\\\\\\\\fi"};x=x.succ;a.length>1?"System.out.println(\"\"\\\\\\\\def \\\\\\\\opt#{x} {#{b[a.dup]}}%\"\");": ""}*"\n"}
System.out.println(""#{x="a";d.map{|a|x=x.succ;a.length>1?"\\\\\\\\opt#{x}{}": a.first}*""}"");
System.out.println();
System.out.println(""#{d.map{|a|a.length>1? "[#{a.map{|b|"'#{b}'"}*""}]#{a.length}rand=" : "'#{a.first}'"}*""}"");
System.out.println(""\\\\\\\\bye"");
}
}");
}
}
]==]);
'''
DATA
__END__
[Thank you/Wow/Great], this is [awesome/wonderful].

Ruby

$ ruby a.rb
Great, this is awesome.
from random import choice
import sys
sys.stdout.write(choice(["Thank you", "Wow", "Great"]))
sys.stdout.write(choice([", this is "]))
sys.stdout.write(choice(["awesome", "wonderful"]))
sys.stdout.write(choice(["."]))
print
print '''math.randomseed(os.time())
a={"Thank you", "Wow", "Great"};io.write(a[math.random(1,3)]);
a={", this is "};io.write(a[math.random(1,1)]);
a={"awesome", "wonderful"};io.write(a[math.random(1,2)]);
a={"."};io.write(a[math.random(1,1)]);
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\\\def \\\\optb {\\\\ifnum500>\\\\pdfuniformdeviate1000 Thank you\\\\else \\\\ifnum500>\\\\pdfuniformdeviate1000 Wow\\\\else Great\\\\fi\\\\fi}%"");

System.out.println(""\\\\def \\\\optd {\\\\ifnum500>\\\\pdfuniformdeviate1000 awesome\\\\else wonderful\\\\fi}%"");

System.out.println(""\\\\optb{}, this is \\\\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\\\bye"");
}
}");
}
}
]==]);
'''

Python

$ python a.py
Great, this is wonderful.
math.randomseed(os.time())
a={"Thank you", "Wow", "Great"};io.write(a[math.random(1,3)]);
a={", this is "};io.write(a[math.random(1,1)]);
a={"awesome", "wonderful"};io.write(a[math.random(1,2)]);
a={"."};io.write(a[math.random(1,1)]);
print()
print([==[
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%"");

System.out.println(""\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%"");

System.out.println(""\\optb{}, this is \\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\bye"");
}
}");
}
}
]==]);

Lua

$ lua a.lua
Great, this is wonderful.
public class a {
public static void Main() {
System.Random r=new System.Random();
if (r.NextDouble()<0.5){System.Console.Write("Thank you");}else{if (r.NextDouble()<0.5){System.Console.Write("Wow");}else{System.Console.Write("Great");};};
System.Console.Write(", this is ");
if (r.NextDouble()<0.5){System.Console.Write("awesome");}else{System.Console.Write("wonderful");};
System.Console.Write(".");
System.Console.WriteLine();
System.Console.WriteLine(@"class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print(""Thank you"");}else{if (r.nextFloat()<0.5){System.out.print(""Wow"");}else{System.out.print(""Great"");};};
System.out.print("", this is "");
if (r.nextFloat()<0.5){System.out.print(""awesome"");}else{System.out.print(""wonderful"");};
System.out.print(""."");
System.out.println();
System.out.println(""\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%"");

System.out.println(""\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%"");

System.out.println(""\\optb{}, this is \\optd{}."");
System.out.println();
System.out.println(""['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'"");
System.out.println(""\\bye"");
}
}");
}
}

C#

$ mcs a.cs
$ mono a.exe
Thank you, this is wonderful.
class a {
public static void main(String[] args) {
java.util.Random r=new java.util.Random();
if (r.nextFloat()<0.5){System.out.print("Thank you");}else{if (r.nextFloat()<0.5){System.out.print("Wow");}else{System.out.print("Great");};};
System.out.print(", this is ");
if (r.nextFloat()<0.5){System.out.print("awesome");}else{System.out.print("wonderful");};
System.out.print(".");
System.out.println();
System.out.println("\\def \\optb {\\ifnum500>\\pdfuniformdeviate1000 Thank you\\else \\ifnum500>\\pdfuniformdeviate1000 Wow\\else Great\\fi\\fi}%");

System.out.println("\\def \\optd {\\ifnum500>\\pdfuniformdeviate1000 awesome\\else wonderful\\fi}%");

System.out.println("\\optb{}, this is \\optd{}.");
System.out.println();
System.out.println("['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'");
System.out.println("\\bye");
}
}

Java

$ javac a.java
$ java a
Wow, this is wonderful.
\def \optb {\ifnum500>\pdfuniformdeviate1000 Thank you\else \ifnum500>\pdfuniformdeviate1000 Wow\else Great\fi\fi}%
\def \optd {\ifnum500>\pdfuniformdeviate1000 awesome\else wonderful\fi}%
\optb{}, this is \optd{}.

['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'
\bye

PdfTeX

(the code will be inside a.pdf instead of the stdout. I hope this is still okay)

$ pdftex a.tex
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012)
 restricted \write18 enabled.
entering extended mode
(./a.tex [1{/usr/local/texlive/2012/texmf-var/fonts/map/pdftex/updmap/pdftex.ma
p}] )</usr/local/texlive/2012/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.p
fb>
Output written on a.pdf (1 page, 15967 bytes).
Transcript written on a.log.
$ open a.pdf

Result:

pdftex result

In text:

Great, this is wonderful.
['Thank you''Wow''Great']3rand=', this is '['awesome''wonderful']2rand='.'

Golfscript

$ ruby golfscript.rb a.gs
Wow, this is awesome.

Notes

  1. It seems PHP is quite forgiving with binary data in string literals, except for hex codes AF and 96. Don't ask why.

  2. I could've probably added perl and C++11 code as well easily (as they both support raw string literals), but 8 is a more round number than 10 :)

  3. The distribution of values is not uniform, as the first element comes more often than later ones. According to comments this should be okay.

  4. The initial code was actually generated using the following PHP 5.5 code. orig.rb is actually the same as the Ruby code without the footer (__END__ and the appended data afterwards)

generator.php:

<?php
$d=file_get_contents('orig.rb');
$dc = gzcompress($d,9);
echo '<?php $x=file_get_contents("php://stdin");echo preg_replace_callback("/\\[(.*?)\\]/",function($m){$r=explode("/",$m[1]);return $r[array_rand($r)];},$x)';
?><?php
echo ".gzuncompress(\"";
for ($i=0;$i<strlen($dc);$i++) {
  switch($dc[$i]) {
  case '"':
    echo "\\\"";break;
  case "\n":
    echo "\\n";break;
  case '\\':
    echo "\\\\";break;
  case "\x00":case "\xaf":case "\x96":
    echo sprintf("\\x%02x",ord($dc[$i]));
    break;
  default:
    echo $dc[$i];
  }
}
echo "\").\"__END__\\n\".\$x;";

(you can potentially use this code to zLib encompress PHP solutions for other challenges)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ ...wow. That's all I have to say :O +1 \$\endgroup\$
    – Doorknob
    Commented Jan 21, 2014 at 2:32
  • \$\begingroup\$ you can omit semicolons in lua, and you can replace a spurious print() call by simply placing another newline after [==[ \$\endgroup\$
    – mniip
    Commented Jan 21, 2014 at 11:24
4
\$\begingroup\$

Ruby 1.9 -> Perl 5 -> Python 2, 227/9 = 25.22222...

$><<(g=gets).gsub(/\[(.*?)\]/){$1.split(?/).sample}+"say'#{g.chop}'=~s|\\[(.*?)\\]|@a=split'/',$1;$a[rand@a]|reg;say'import re,random;print re.sub(\"\\[(.*?)\\]\",lambda m:random.choice(m.group(1).split(\"/\")),\"#{g.chop}\")'"

Output for [Please/Kindly] [visit/see] my [site/blog].:

Please see my site.
say'[Please/Kindly] [visit/see] my [site/blog].'=~s|\[(.*?)\]|@a=split'/',$1;$a[rand@a]|reg;say'import re,random;print re.sub("\[(.*?)\]",lambda m:random.choice(m.group(1).split("/")),"[Please/Kindly] [visit/see] my [site/blog].")'

The Perl program (my first ever! :-P) outputs:

Kindly see my blog.
import re,random;print re.sub("\[(.*?)\]",lambda m:random.choice(m.group(1).split("/")),"[Please/Kindly] [visit/see] my [site/blog].")

The Python program outputs:

Kindly visit my site.
\$\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.