Write a Palindrome-Polyglot-Quine

25

4

Write a "palipolyquine": a program that is a quine, a polyglot, and a palindrome.

Rules:

  • The number of polyglot languages is more preferable than code size.
  • The shortest answer (in bytes) wins, in case of a tie.
  • Polyglot and Quine rules see here: Write a Polyquine.

My example (I have a repository Freaky-Sources with tests):

C#/Java (1747 bytes):

/**///\u000A\u002F\u002A
using System;//\u002A\u002F
class Program{public static void//\u000A\u002F\u002A
Main//\u002A\u002Fmain
(String[]z){String s="`**?`@#_^using System;?_#^class Program{public static void?@#_^Main?_#main^(String[]z){String s=!$!,t=s;int i;int[]a=new int[]{33,94,38,64,35,95,96,63,36};String[]b=new String[]{!&!!,!&n!,!&&!,!&@!,!&#!,!&_!,!`!,!?!,s};for(i=0;i<9;i++)t=t.?@#_^Replace?_#replace^(!!+(char)a[i],b[i]);t+='*';for(i=872;i>=0;i--)t=t+t?@#_^[i];Console.Write?_#.charAt(i);System.out.printf^(t);}}/",t=s;int i;int[]a=new int[]{33,94,38,64,35,95,96,63,36};String[]b=new String[]{"\"","\n","\\","\\u000A","\\u002F","\\u002A","/","//",s};for(i=0;i<9;i++)t=t.//\u000A\u002F\u002A
Replace//\u002A\u002Freplace
(""+(char)a[i],b[i]);t+='*';for(i=872;i>=0;i--)t=t+t//\u000A\u002F\u002A
[i];Console.Write//\u002A\u002F.charAt(i);System.out.printf
(t);}}/*/}};)t(
ftnirp.tuo.metsyS;)i(tArahc.F200u\A200u\//etirW.elosnoC;]i[
A200u\F200u\A000u\//t+t=t)--i;0=>i;278=i(rof;'*'=+t;)]i[b,]i[a)rahc(+""(
ecalperF200u\A200u\//ecalpeR
A200u\F200u\A000u\//.t=t)++i;9<i;0=i(rof;}s,"//","/","A200u\\","F200u\\","A000u\\","\\","n\",""\"{][gnirtS wen=b][gnirtS;}63,36,69,59,53,46,83,49,33{][tni wen=a][tni;i tni;s=t,"/}};)t(^ftnirp.tuo.metsyS;)i(tArahc.#_?etirW.elosnoC;]i[^_#@?t+t=t)--i;0=>i;278=i(rof;'*'=+t;)]i[b,]i[a)rahc(+!!(^ecalper#_?ecalpeR^_#@?.t=t)++i;9<i;0=i(rof;}s,!?!,!`!,!_&!,!#&!,!@&!,!&&!,!n&!,!!&!{][gnirtS wen=b][gnirtS;}63,36,69,59,53,46,83,49,33{][tni wen=a][tni;i tni;s=t,!$!=s gnirtS{)z][gnirtS(^niam#_?niaM^_#@?diov citats cilbup{margorP ssalc^#_?;metsyS gnisu^_#@`?**`"=s gnirtS{)z][gnirtS(
niamF200u\A200u\//niaM
A200u\F200u\A000u\//diov citats cilbup{margorP ssalc
F200u\A200u\//;metsyS gnisu
A200u\F200u\A000u\///**/

Compilation available on ideone.com: C#, Java.

Ivan Kochurkin

Posted 2016-09-15T11:37:57.533

Reputation: 501

2Are there any rules involved in making the poly-quine? Any limitations? If this question is as-is, then a possible (trivial) solution is 1 (which will output 1 in many languages here, and is palindromic). – clismique – 2016-09-15T11:39:40.500

I added more formal rules. – Ivan Kochurkin – 2016-09-15T11:48:07.397

2Ah, cool. Well, good luck on your first challenge! Just some more things though: you don't really need the code block there, and you should probably migrate the rules from the other challenge over to here. – clismique – 2016-09-15T11:52:20.073

1Does a 5-language, 999-byte answer beat a 4-language, 100-byte answer? – ETHproductions – 2016-09-15T13:15:05.163

@ETHproductions, I think yes. It's harder to add new language than reduce number of lines. – Ivan Kochurkin – 2016-09-15T13:20:51.197

i say that 100 bytes solutions in too high languages are 1000 times slower than the C or assembly one 999 bytes – RosLuP – 2016-09-15T17:01:53.557

Just a suggestion...What about Help, WarDoq! as one language? At least with Q it outputs itself and ignores a lot of characters that might be used in other languages.

– geisterfurz007 – 2016-11-17T07:06:17.020

Answers

14

CJam/GolfScript, 2 languages, 50 bytes

{`"0$~e#"+0$-1%"":n}0$~e##e~$0}n:""%1-$0+"#e~$0"`{

Try it CJam! Try it in GolfScript!

Huh, this went unanswered surprisingly long.

Explanation

It's probably easiest to explain this by showing how I turned the basic quine in each language into a palindromic polyglot quine.

So the basic quines in both languages are:

{".~"}.~

{"_~"}_~

In GolfScript and CJam, respectively. These are quite similar thanks to the fact that CJam was originally inspired by GolfScript (but has since deviated quite a lot). The first difference we notice is that one uses . for duplicating the top of the stack and the other uses _. A common trick to avoid this problem is to use 0$, since both languages have the "copy-nth-item-on-stack" operator $. So we get {"0$~"}0$~, although that still needs a trailing linefeed in GolfScript. But let's worry about that at the end.

First, we need to make it a palindrome. The obvious solution to this is to append a comment and put the source code there in reverse. This is quite simple, because CJam uses e# for comments, and in GolfScript e does nothing at all, and # is a comment. So if we append e#... that works for both languages. Here is what we've got:

{"0$~"}0$~e##e~$0}"~$0"{

Of course, that doesn't actually print the part from e# onward. We can reconstruct this quite easily from the source code itself. Both languages can turn the initial block into a string with ` and append the "0$~" part with +, so that we get the entire unmirrored source code in a single string. To append a mirrored copy, all we need to do is duplicate the string with 0$ again and then reverse it with -1%, which also works in both languages. So now we've got this:

{`"0$~e#"+0$-1%}0$~e##e~$0}%1-$0+"#e~$0"`{

This is a valid palindromic quine in CJam, and it also works in GolfScript but still prints that pesky trailing linefeed.

The usual way to prevent this is to assign an empty string to n, because what GolfScript really does is print the contents of n at the end. So what we need is "":n. So what about CJam? Thankfully, this does nothing at all. "" is also an empty string (or empty list, they're the same thing in CJam), and : maps the operator n (print with linefeed) over the list. But since the list is empty, mapping an operator over it does nothing at all. Hence, we can get rid of the linefeed, without messing with CJam, and end up with the final solution:

{`"0$~e#"+0$-1%"":n}0$~e##e~$0}n:""%1-$0+"#e~$0"`{

Martin Ender

Posted 2016-09-15T11:37:57.533

Reputation: 184 808

5

Perl 5/Ruby/PHP/JavaScript (Browser), 4 languages, 513 bytes

$_='$z=0?"$&".next: eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);//#//;)_%s(lave;%s%s%s=_%s",$d=$z[0]||h^L,$q=$z[1]||h^O,$_,$q,$d,$d,$q,"0"?$_.split("").reverse().join(""):~~reverse,$q,$d)';eval($_);//#//;)_$(lave;')d$,q$,esrever~~:)""(nioj.)(esrever.)""(tilps._$?"0",q$,d$,d$,q$,_$,O^h||]1[z$=q$,L^h||]0[z$=d$,"s%_=s%s%s%;eval(s%_);//#//;)_s%(lave;s%s%s%=_s%"(ftnirp;)"`cCJ`bota;gol.elosnoc=ftnirp"(lave :txen."&$"?0=z$'=_$

Try the Perl online!
Try it online!
Try the PHP online!
Validate it online!

$_='$z=0?"$&".next: eval("printf=console.log;atob`JCc`");printf("%s_=%s%s%s;eval(%s_);//#//;)_%s(lave;%s%s%s=_%s",$d=$z[0]||h^L,$q=$z[1]||h^O,$_,$q,$d,$d,$q,"0"?$_.split("").reverse().join(""):~~reverse,$q,$d)';eval($_);//#//;)_$(lave;')d$,q$,esrever~~:)""(nioj.)(esrever.)""(tilps._$?"0",q$,d$,d$,q$,_$,O^h||]1[z$=q$,L^h||]0[z$=d$,"s%_=s%s%s%;eval(s%_);//#//;)_s%(lave;s%s%s%=_s%"(ftnirp;)"`cCJ`bota;gol.elosnoc=ftnirp"(lave :txen."&$"?0=z$'=_$

Dom Hastings

Posted 2016-09-15T11:37:57.533

Reputation: 16 415