Write a self-aware program

1

A standard (but disputed) test of self-awareness is the mirror test: An animal is considered self aware if it can recognize itself in the mirror.

Your task is to write a program that is self-aware according to this test. More exactly:

  • The program reads input in whatever form is suitable for your language.

  • If the input contains a mirror image of its own source code, it produces a truthy value, otherwise a falsey value.

  • A valid mirror image is the exact source of the program with the order of characters reversed, with no extra characters in between. As a special exception, for systems/languages that don't use single characters to terminate/separate lines (e.g. using a CR/LF sequence), the line termination sequence is allowed, but not required to be kept intact on reversal. However the program must be consistent in its treatment of lines.

    For example, on a system using CR/LF as line separator, the sequence AB<cr><lf>CD may be reversed either to DC<cr><lf>BA (keeping the line ending intact) or to DC<lf><cr>BA (completely reversing the character sequence).

  • The mirror image must be detected also if it is preceded or followed by junk, even if that junk contains reversed fragments of the code.

This is code golf; the shortest entry wins.

Note this is not the same as this question because of (a) the reversal of the code, and (b) the fact that it has to tolerate junk before and after the reversed code, including junk that is very similar to the actual reversed code.

celtschk

Posted 2016-10-15T21:44:44.607

Reputation: 4 650

Question was closed 2016-10-15T22:18:03.473

5Borderline duplicate. – Martin Ender – 2016-10-15T21:50:22.463

@MartinEnder I'd call it a dupe. – xnor – 2016-10-15T21:51:40.350

Note about generalized quines.

– xnor – 2016-10-15T21:54:00.470

@xnor I was just about to post that before I realised that Retina does actually have a solution that isn't based on the standard quine. – Martin Ender – 2016-10-15T21:56:05.077

Please use the Sandbox in the future to get feedback on your challenges before posting them to the main site.

– Mego – 2016-10-15T21:57:59.300

Just out of curiosity, would the following JS answer have been considered as valid according to the current quine rules? (I'm not familiar with them.) s=>~s.indexOf([...unescape(q='s=>~s.indexOf([...unescape(q=%270%27).replace(0,q)].reverse().join\`)').replace(0,q)].reverse().join``)` – Arnauld – 2016-10-15T22:23:28.563

Answers

1

Retina, 1 byte

x

Try it online!

Prints a positive integer when the input contains at least one "reversed" copy of the source and 0 otherwise.

Any character that isn't a backtick or a regex metacharacter ([()?*+\\^$.) would work.

Martin Ender

Posted 2016-10-15T21:44:44.607

Reputation: 184 808

Interesting solution, but don't we require quines to be proper now?

– xnor – 2016-10-15T21:58:34.563

@xnor If we extend that definition to generalised quines, then don't all solutions have to be extensions of a standard quine? I figured the only way to have interesting solutions to those is to find a way to circumvent that. (Especially since there's no way to use this approach for a standard quine in Retina, nor for most generalised quines.) – Martin Ender – 2016-10-15T22:00:58.857