11
\$\begingroup\$

Quines are fun. Polyglots are fun, too. Polyglot Quines exist, but we can raise the bar even higher.

Write a file that contains a valid program for languages α, β, and γ. When the file is executed (possibly after compiling it) as a language α or β program, the program's output shall be of the same form as a valid submission to this contest. If your file is executed as a language γ program, it shall output a number. The value of this number is the chain of previous executions of the program interpreted as a binary number.

This explanation might be a bit hard to understan, so here is an example. Let Α, Β, and Γ be functions that execute their input as a language α, β, or γ resp. program and return the output of these programs. Let x be a valid submission to this contest. Then the following expression, where we process x through language β, α, β, α, α, β, and γ in this order, shall yield 41, since 4110 = 1010012.

Γ(Β(Α(Α(Β(Α(Β(x)))))))

You may not assume that penultimate execution in the chain is an execution in language β. For the case where your original submission is directly executed as a language γ program, it shall print 0.

Your program shall behave correctly for up to sixteen compilations in the chain; that is, the highest number your program might print out at the end is 215 - 1. Of course, your program is allowed to support longer compilation chains.

This is a popularity contest to encourage creative solutions. The submission with the highest vote tally wins.

\$\endgroup\$
9
  • \$\begingroup\$ Can the three languages be equal (even though it would drastically reduce upvotes)? \$\endgroup\$
    – Zgarb
    Feb 12, 2015 at 13:07
  • 6
    \$\begingroup\$ @Zgarb I'd love to see a solution where some (or all) of the languages are equal. Let's see how you manage to distinguish equal languages. \$\endgroup\$
    – FUZxxl
    Feb 12, 2015 at 13:09
  • 1
    \$\begingroup\$ ...I see. :D I read the challenge too hastily. \$\endgroup\$
    – Zgarb
    Feb 12, 2015 at 13:11
  • \$\begingroup\$ @Zgarb how about different versions of the same language? magic += Number(System.env.lang_version[-1]) \$\endgroup\$ Feb 12, 2015 at 16:11
  • 1
    \$\begingroup\$ +1 for using greek letters instead of the boring a,b,c or 1,2,3 =) \$\endgroup\$
    – flawr
    Feb 13, 2015 at 10:33

1 Answer 1

13
\$\begingroup\$

Python 2, Python 3, ><> (Fish)

#;n0
import sys
x='\\\'\nn#;n0import sysx=v=int(1/2*2)sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])n'
v=int(1/2*2)
sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])

Python explanation

The Python 2 and Python 3 interpreters work similarly except the v=int(1/2*2) variable gets different values (0 and 1) as Python 2 uses float division and Python 3 uses integer division.

In every run they add the expression +0*2 or +1*2 to the first line (after #;n) and to the x string (after the last write command). The ><> interpreter uses the first addition and the Pythons use the second one to create correct quines.

Code after B(A(B(B(x)))):

#;n+1*2+0*2+1*2+1*20
import sys
x='\\\'\nn#;n0import sysx=v=int(1/2*2)sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])+1*2+0*2+1*2+1*2n'
v=int(1/2*2)
sys.stdout.write(x[4:7]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[7:8]),sys.stdout.write(x[2:3]),sys.stdout.write(x[8:18]),sys.stdout.write(x[2:3]),sys.stdout.write(x[18:20]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[0:1]),sys.stdout.write(x[1:2]),sys.stdout.write(x[0:1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[3:4]),sys.stdout.write(x[4:7]),sys.stdout.write(x[7:8]),sys.stdout.write(x[8:18]),sys.stdout.write(x[18:20]),sys.stdout.write(x[20:32]),sys.stdout.write(x[32:851]),sys.stdout.write(chr(43)+str(v)+chr(42)+chr(50)),sys.stdout.write(x[851:-1]),sys.stdout.write(x[3:4]),sys.stdout.write(x[1:2]),sys.stdout.write(x[2:3]),sys.stdout.write(x[20:32]),sys.stdout.write(x[2:3]),sys.stdout.write(x[32:851])

><> (Fish) explanation

When you run the ><> interpreter the code pointer bounces back from the # wraps around the first line and starting from the end of the first line and heading West starts pushing numbers onto the stack. If an operator comes (+ or *) it pops the top two elements from the stack and pushes back the result. With this method we end up with the base2 representation of the previous runs (13 in the former example). This is the desired number so we output it with n and terminate with ;.

\$\endgroup\$
1
  • \$\begingroup\$ This is pretty nice. \$\endgroup\$
    – FUZxxl
    Feb 13, 2015 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.