Pseudo-quine polyglot language-detection counters

11

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.

FUZxxl

Posted 2015-02-12T13:04:37.137

Reputation: 9 656

Can the three languages be equal (even though it would drastically reduce upvotes)? – Zgarb – 2015-02-12T13:07:19.083

6@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. – FUZxxl – 2015-02-12T13:09:31.123

1...I see. :D I read the challenge too hastily. – Zgarb – 2015-02-12T13:11:20.233

@Zgarb how about different versions of the same language? magic += Number(System.env.lang_version[-1]) – John Dvorak – 2015-02-12T16:11:19.930

@JanDvorak Possible, but unless it's really crafty I don't think you're going to get many upvotes. A bit boring. – FUZxxl – 2015-02-12T19:49:04.797

Are we allowed to read source file or access external resources? – randomra – 2015-02-13T02:54:05.100

@randomra that would count as a standard loophole, I'm afraid. – John Dvorak – 2015-02-13T05:10:59.397

@FUZxxl in the land of the blind, the one-eyed man is king ;-) – John Dvorak – 2015-02-13T05:17:13.887

1+1 for using greek letters instead of the boring a,b,c or 1,2,3 =) – flawr – 2015-02-13T10:33:26.167

Answers

13

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 ;.

randomra

Posted 2015-02-12T13:04:37.137

Reputation: 19 909

This is pretty nice. – FUZxxl – 2015-02-13T13:08:09.207