Backwards and forwards polyglot quine!

9

1

You must make a polyglot that outputs its source code in one language and its source code backward in another. Unlike the normal rules, you are allowed to read the current file or use a builtin to get the source code of your submission and reverse that in one language. Your source code cannot be a palindrome.

For example, if your source code is abcxyz, it must output abcxyz in one language and zyxcba in another. If your code is abcxyzyxbca, it's invalid because it's a palindrome.

Good luck!

programmer5000

Posted 2017-04-24T11:29:13.090

Reputation: 7 828

3Normal rules are there for a reason. Allowing quine built-ins will likely make this challenge too broad, and allowing palindrome source codes allows answers which are forward quines for both languages. – Erik the Outgolfer – 2017-04-24T11:43:39.600

@EriktheOutgolfer palindromes are now not allowed. – programmer5000 – 2017-04-24T11:44:58.760

Palindromes are not allowed What does that mean? Are you trying to forbid built-in functions that reverse their input, maybe? – Luis Mendo – 2017-04-24T11:50:24.417

@LuisMendo No I suggested that if palindromes are allowed then a forward quine+forward quine polyglot could be a valid answer (while not being the intention). Reverse functions aren't banned. – Erik the Outgolfer – 2017-04-24T11:51:26.980

1@EriktheOutgolfer Ah, so the source code cannot be a palindrome? – Luis Mendo – 2017-04-24T11:52:45.547

@programmer5000 Well, I don't think it's too broad anymore. – Erik the Outgolfer – 2017-04-24T11:52:45.710

@LuisMendo Yeah, I'll clarify and add an example in there. – Erik the Outgolfer – 2017-04-24T11:53:14.903

@programmer5000 Are different versions of a language considered the same language or not? – Erik the Outgolfer – 2017-04-24T11:57:33.880

@LuisMendo you are correct. – programmer5000 – 2017-04-24T12:07:35.337

@ErikTheOutgolfer different versions of the same language are allowed. – programmer5000 – 2017-04-24T12:08:04.110

2I think you should offer a bounty for the first person to complete this challenge without breaking any of the normal quine rules. (maybe 50 rep?) – clismique – 2017-04-24T12:13:46.810

@Qwerp-Derp good idea. Maybe. – programmer5000 – 2017-04-24T12:15:39.607

1I read "in one language" as clearly disallowing the case where both languages read the source code, but the current top-voted answer does exactly that. Can you edit to make it clear whether that's meant to be allowed? – hvd – 2017-04-24T21:44:03.440

Answers

5

PHP & GolfScript, 2 bytes

1

i.e. a newline and the digit 1.

This is a reverse quine in GolfScript, contributed on this site by Justin. PHP hasn't triggered that it's a programming language so it prints its input.

Gilles 'SO- stop being evil'

Posted 2017-04-24T11:29:13.090

Reputation: 2 531

6

Batch / bash + tac, 39 bytes

:;tac -r -s '.\|'$'\n' $0;exit
@type %0

Outputs forwards in Batch. Explanation: Batch sees the first line as a label and ignores it, while the second line simply copies the source file to STDOUT. (Note that you need to invoke the file including extension, or change %0 to %~f0.) bash sees four commands:

  • : does nothing (same as true)
  • tac -r -s '.\|'$'\n' $0
    • -r puts tac into regex mode
    • -s specifies a regex
    • '.\|'$'\n' is the regex, composed of
      • . any character except newline
      • \| or
      • $'\n' a newline
    • The upshot is that tac splits the file into characters rather than lines.
  • exit stops the script, ignoring the fourth command
  • @type %0 (ignored)

Neil

Posted 2017-04-24T11:29:13.090

Reputation: 95 035

I think that you can replace the first line with :;rev $0|tac;exit. Also, the header should say Batch / sh + util-linux + coreutils instead. – Erik the Outgolfer – 2017-04-24T12:01:48.377

@EriktheOutgolfer $'\n' is a Bashism, rev $0|tac doesn't reverse newlines correctly, and does it really matter that tac comes in a package? – Neil – 2017-04-24T12:23:36.993

$'\n' doesn't exist in the replacement I've suggested, and rev $0|tac works for me. And I think it's better to specify the packages instead of the individual utilities, because sometimes confusion might arise (e.g. which package's tac?). – Erik the Outgolfer – 2017-04-24T12:29:01.710

@EriktheOutgolfer Try it online! is wrong. So is Try it online! (but more subtly).

– Neil – 2017-04-24T12:31:52.853

Could you please give an explanation? – programmer5000 – 2017-04-24T12:34:41.333

@Neil Oh I see, I was tricked by a trailing newline. – Erik the Outgolfer – 2017-04-24T12:45:25.963

@programmer5000 Something like that? – Neil – 2017-04-24T13:04:48.723

6

Python 2 / Python 3, 71 bytes

lambda _='lambda _=%r:(_%%_)[::int(1-(1/2)*4)]':(_%_)[::int(1-(1/2)*4)]

Does not use any quine builtins.

Thanks to ovs for generally awakening me.

Erik the Outgolfer

Posted 2017-04-24T11:29:13.090

Reputation: 38 134

lambda _='lambda _=%r:(_%%_)[::int(1-(1/2)*4)]':(_%_)[::int(1-(1/2)*4)] for 71 bytes – ovs – 2017-04-24T12:48:09.467

@ovs Oh of course, how I didn't think of that. – Erik the Outgolfer – 2017-04-24T12:49:05.183

3

PHP & Retina, 2 bytes

1

The same as Gille's PHP & GolfScript answer.

PHP just reads the code and outputs it directly, Retina will replace matches of an empty string in the input and replace it with 1, and output that with a newline.

Okx

Posted 2017-04-24T11:29:13.090

Reputation: 15 025

2

JS (ES5), JS (ES6), 94 bytes

function f(){try{eval("x=(f+'f()').split``.reverse().join``")}catch(e){x=f+"f()"};return x}f()

Does not use any quine built-ins. Just uses the fact that JS functions stringify to their code. Probably can be golfed more.

programmer5000

Posted 2017-04-24T11:29:13.090

Reputation: 7 828

1

If you allow reading the source code, it isn't really a quine.

PHP & sh+util-linux, 6 bytes

rev $0

I'm sure that the usual golfing languages can do it in 2 bytes.

Gilles 'SO- stop being evil'

Posted 2017-04-24T11:29:13.090

Reputation: 2 531

They infact can. – ATaco – 2017-04-26T00:16:25.903