JavaScript + Python 3 + Bash, 938 bytes
JavaScript, 465 bytes
a=3-2-1//True;a=str(a);alert,eval,prompt=print,exec,input;String=JSON=__builtins__;JSON.stringify=repr;JSON.fromCharCode=chr
s=String.fromCharCode;j=JSON.stringify;r="t='tr '+s(48)+s(49)+' '+s(49)+s(48)";eval(r);d="alert([t,'a=3-2-'+a+'//True;a=str(a);alert,eval,prompt=print,exec,input;String=JSON=__builtins__;JSON.stringify=repr;JSON.fromCharCode=chr\\ns=String.fromCharCode;j=JSON.stringify;r='+j(r)+';eval(r);d='+j(d)+';eval(d)'][+(prompt()[2]<s(33))])";eval(d)
Python 3, 465 bytes
a=3-2-0//True;a=str(a);alert,eval,prompt=print,exec,input;String=JSON=__builtins__;JSON.stringify=repr;JSON.fromCharCode=chr
s=String.fromCharCode;j=JSON.stringify;r="t='tr '+s(48)+s(49)+' '+s(49)+s(48)";eval(r);d="alert([t,'a=3-2-'+a+'//True;a=str(a);alert,eval,prompt=print,exec,input;String=JSON=__builtins__;JSON.stringify=repr;JSON.fromCharCode=chr\\ns=String.fromCharCode;j=JSON.stringify;r='+j(r)+';eval(r);d='+j(d)+';eval(d)'][+(prompt()[2]<s(33))])";eval(d)
Bash, 8 bytes
tr 01 10
TIO links
Javascript(Bash)
Javascript(Python)
Python(Bash)
Python(Javascript)
Bash(Javascript)
Bash(Python)
For some reason, TIO doesn't understand Javascript's alert
function, but you can try it in your browser console.
Explanation:
The JavaScript and Python codes are 1 byte apart. The only difference is that the JS code starts with a=3-2-1
where the Python code has a=3-2-0
. This is a polyglot pseudo-quine. The output is almost identical to the source code, but if the source code has a 1
in that statement, the output has a 0
, and vice versa. This is because the pseudo-quine puts the current value of a
into the string that describes how a
is initialized.
In other words, the JavaScript code constructs the Python code and vice versa, and it's completely arbitrary which one is considered JavaScript and which is considered Python. Both programs are valid in either language.
Here's the JS/Python code without the quine construction, and with spaces and line breaks to make it more readable:
a=3-2-1//True; a=str(a); alert,eval,prompt=print,exec,input; String=JSON=__builtins__; JSON.stringify=repr; JSON.fromCharCode=chr
s=String.fromCharCode;
j=JSON.stringify;
r="t='tr '+s(48)+s(49)+' '+s(49)+s(48)";
eval(r);
d="alert([t, <pseudo-quine>][+(prompt()[2]<s(33))])";
eval(d)
The JavaScript version ignores all the redefinitions after //
on the top line, and redefines the verbose String.fromCharCode
and JSON.stringify
functions to single characters. It then constructs the string tr 01 10
and the pseudo-quine and puts both in a list. To access the list, it checks whether the third character in the input is less than !
and casts the result to an integer with +(bool)
. If the input is tr 01 10
(or anything else with a space as the third character), this check is true and it prints the pseudo-quine. If the input is the Python code, this condition is false, and it prints tr 01 10
.
The Python logic is identical, but it renames some functions and creates dummy JSON
and String
namespaces for compatibility with JavaScript. It also casts a
to a string so Python won't complain about concatenation. The redefinitions are possible because //
is a comment in JavaScript, but floor division in Python. a//True
is the same as a//1
, but I'm avoiding the use of 1
and 0
everywhere except the first statement.
Finally, tr 01 10
simply changes 0
to 1
and vice versa. This has the same effect as running the JS/Python code with the other version as input.
I initially chose Python and Javascript because they have a lot of overlapping syntax and //
made it easy to add compatibility logic, but I ended up having to redefine more functions than I expected. There's probably a better pair of languages for this approach (maybe Python and Ruby?). I guess I could have cheated by using Python 2 and 3 as my "different languages", but that's hardly the most interesting approach :)