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.

  • 5
    \$\begingroup\$ Borderline duplicate. \$\endgroup\$ Commented Oct 15, 2016 at 21:50
  • \$\begingroup\$ @MartinEnder I'd call it a dupe. \$\endgroup\$
    – xnor
    Commented Oct 15, 2016 at 21:51
  • \$\begingroup\$ Note about generalized quines. \$\endgroup\$
    – xnor
    Commented Oct 15, 2016 at 21:54
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Oct 15, 2016 at 21:56
  • \$\begingroup\$ Please use the Sandbox in the future to get feedback on your challenges before posting them to the main site. \$\endgroup\$
    – user45941
    Commented Oct 15, 2016 at 21:57

1 Answer 1


Retina, 1 byte


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.

  • \$\begingroup\$ Interesting solution, but don't we require quines to be proper now? \$\endgroup\$
    – xnor
    Commented Oct 15, 2016 at 21:58
  • \$\begingroup\$ @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.) \$\endgroup\$ Commented Oct 15, 2016 at 22:00

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