71
\$\begingroup\$

Write a program that will generate a "true" output iff the input matches the source code of the program, and which generates a "false" output iff the input does not match the source code of the program.

This problem can be described as being related to quines, as the program must be able to somehow compute its own source code in the process.

This is code golf: standard rules apply. Your program must not access any special files, such as the file of its own source code.

Edit: If you so choose, true/false can be replaced with True/False or 1/0.

Example

If the source code of your program is bhiofvewoibh46948732));:/)4, then here is what your program must do:

Input (Stdin)

bhiofvewoibh46948732));:/)4

Output (Stdout)

true

Input

(Anything other than your source code)

Output

false
\$\endgroup\$
5
  • 7
    \$\begingroup\$ Is the true/false output a strong requirement, or are variations (True/False, 1/0) acceptable as well? \$\endgroup\$ Commented Apr 17, 2013 at 14:54
  • \$\begingroup\$ Is it a problem if the program outputs a little more than true/false (if it keeps being unambiguous and ends with true/false) ? \$\endgroup\$ Commented Apr 19, 2013 at 11:31
  • 1
    \$\begingroup\$ Related: Interpret your lang, but not yourself? \$\endgroup\$ Commented Jan 11, 2014 at 5:28
  • 7
    \$\begingroup\$ So you mean a Narcissist program? \$\endgroup\$ Commented Jul 15, 2017 at 5:57
  • \$\begingroup\$ Very much related \$\endgroup\$
    – Deadcode
    Commented Jan 20, 2020 at 7:37

48 Answers 48

1
2
1
\$\begingroup\$

Runic, 11 bytes

"3X4+kSqi=@

Try it online!

TIO got updated and there's no longer an issue reading input (and no longer requires a trailing whitespace).

Explanation

>                 Implicit entry
 "                Begin reading as string
  3X4+kSqi=@      Pushed to the stack as a string, loop around
 "                End reading as string
  3X4+            Push 3*10 and 4 to the stack, add them together
      k           Convert to character (")
       S          Swap the top two items on the stack
        q         Concatenate. This leaves only "3X4+kSqi=@ on the stack
         i        Read input
          =       Compare using .Equals, push 1 if equal, else 0
           @      Print and terminate

JoKing's solution:

"'<~qi=@|

Explanation

  <              Entry
 '               Read character (loop around)
"                Push "
         |       Mirror
"                Begin reading string (loop around)
 '<~ri=@|        Push the string '<~qi=@| (loop around)
"                End reading string
 '<~             Push the character < and then discard it
    q            Concatenate, stack contains only "'<~qi=@|
      i          Read input
       =         Compare
        @        Print and terminate
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 10 bytes \$\endgroup\$
    – Jo King
    Commented Sep 30, 2018 at 6:37
  • \$\begingroup\$ @JoKing Very clever. \$\endgroup\$ Commented Sep 30, 2018 at 15:00
  • \$\begingroup\$ Actually, 9 bytes avoids the reverse \$\endgroup\$
    – Jo King
    Commented Sep 30, 2018 at 15:24
  • \$\begingroup\$ @JoKing I probably should've been able to arrive at that (from the 10 byte solution) myself, but I haven't had my cawfee yet. I'd already worked out yesterday that having the " on the left is the only place it can really go, because having it elsewhere complicates things. (But just now I had to run it in my debugger to see what it was doing...) \$\endgroup\$ Commented Sep 30, 2018 at 15:40
1
\$\begingroup\$

05AB1E, 15 bytes

0"D34çýQ"D34çýQ

Modifies the default 0"D34çý"D34çý by adding Q (check for equality with the implicit input)

Try it online.

Explanation:

0                # Push 0 to the stack
                 #  STACK: [0]
 "D34çýQ"        # Push the string 'D34çýQ' to the stack
                 #  STACK: [0, 'D34çýIå']
         D       # Duplicate this string
                 #  STACK: [0, 'D34çýIå', 'D34çýIå']
          34ç    # Push '"' to the stack
                 #  STACK: [0, 'D34çýIå', 'D34çýIå', '"']
             ý   # Join the stack by this '"' delimiter
                 #  STACK: ['0"D34çýIå"D34çýIå']
              Q  # Check if it's equal to the (implicit) input
                 # (and output the top of the stack implicitly as result)

Cool 15 bytes alternative provided by @Grimy:

187745012D27BJQ

Try it online.

Explanation:

187745012        # Push integer 187745012 
                 #  STACK: [187745012]
         D       # Duplicate it
                 #  STACK: [187745012, 187745012]
          27     # Push integer 27
                 #  STACK: [187745012, 187745012, 27]
            B    # Convert 187745012 to base-27
                 #  STACK: [187745012, "D27BJQ"]
             J   # Join the values on the stack together
                 #  STACK: ["187745012D27BJQ"]
              Q  # Check if it's equal to the (implicit) input
                 # (and output the top of the stack implicitly as result)
\$\endgroup\$
1
  • 3
    \$\begingroup\$ 187745012D27BJQ is a tie. \$\endgroup\$
    – Grimmy
    Commented Jun 13, 2019 at 12:21
1
\$\begingroup\$

Perl 6, 33 bytes

<dd "<$_>~~.EVAL"eq slurp>~~.EVAL

Try it online!

Outputs either Bool::True or Bool::False to STDERR. This is the basic quine format, with an added check against input (eq slurp).

\$\endgroup\$
1
\$\begingroup\$

Keg, 36 28 26 bytes

`:&\`ⁿ^\`ⁿ&⅀=`:&\`ⁿ^\`ⁿ&⅀=

Try it online!

Same concept as before, but shorter.

Answer History

`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=

Try it online!

Who knew generating a program that knows itself would be so unreadable!

Explained

The very first step here is to generate the string that will be checked against. At this point, we don't particularly know what the checking part will look like, but we do know that there will be a string that looks like so:

`\`ⁿ⅍^\`ⁿ⅍++`

Which would consequently be followed by:

 \`ⁿ⅍^\`ⁿ⅍++

This creates a string which contains itself quoted... Something needed for when input is taken, as the quotes are going to be present.

From here, string equality is quite easy, coming in at a trivial 2 bytes:

¿=

Incorporating this into the string gives:

`\`ⁿ⅍^\`ⁿ⅍++¿=`\`ⁿ⅍^\`ⁿ⅍++¿=

At this stage, one might think the quine is complete... It has the string part and it has the equality part... What else could be needed? Well. Let me tell you we ain't done yet.

Y'see, we have a quoted string on the stack, but we don't have it's unquoted equivalent present, meaning we need to account for that. The register works well here:

`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=

And there you have it: a self identifying program written completely on a phone keyboard.

\$\endgroup\$
2
  • \$\begingroup\$ It still outputs 1 if the input is followed by a newline and then anything else \$\endgroup\$
    – Jo King
    Commented Jan 19, 2020 at 23:32
  • \$\begingroup\$ Well of course it would, because Keg only reads one line at a time. I'll have to clarify input methods with Keg, because otherwise multi line input isn't possible. \$\endgroup\$
    – lyxal
    Commented Jan 19, 2020 at 23:38
1
\$\begingroup\$

Japt, 22 16 14 bytes

"iQ ²¶U"iQ ²¶U

Try it online!

Explanation

"iQ ²¶U"iQ ²¶U
"iQ ²¶U"        // Take this string.        iQ ²¶U
        iQ      // Insert a quote.          "iQ ²¶U
           ²    // Double.                  "iQ ²¶U"iQ ²¶U
            ¶U  // Check if equal to input.
\$\endgroup\$
1
\$\begingroup\$

#PowerShell, 28 bytes

Very similar to this JavaScript answer, uses function provider.

filter f{"$Function:f"-eq$_}

#Example

PS > '"$Function:f"-eq$_' | f
True

PS > 'xxx' | f
False
\$\endgroup\$
1
\$\begingroup\$

Ruby -0n, 32 27 bytes

Saved 5 bytes thanks to a comment by @Sisyphus on another answer.

eval s="p$_=='eval s=%p'%s"

Try it online!

Disclosure: very similar to my answer on a duplicate question.

The -0 flag sets the null byte as the input record separator. Without -0, each line of the input would be read (and tested for equality with the code) separately.

\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 48 bytes

(a=:(show(ARGS[] == "(a=:($(a)))|>eval")))|>eval

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 132 bytes

#define Q(X)main(c,v)int**v;{return!strcmp(#X"\nQ("#X")",v[1]);}
Q(#define Q(X)main(c,v)int**v;{return!strcmp(#X"\nQ("#X")",v[1]);})

Try it online!

It turns out using a macro works out better than using (a)sprintf, even with %c%s%1$c.

I'm not sure if this type of input (a single multiline command-line argument) and output (return value of 1 for true or 0 for false) are allowed.

If they aren't:


C (gcc), 152 bytes

x,y[99];main(z){asprintf(&x,z="x,y[99];main(z){asprintf(&x,z=%c%s%1$c,34,z);gets(y);putchar(48+!strcmp(x,y));}",34,z);gets(y);putchar(48+!strcmp(x,y));}

Try it online!

Assumes an ASCII environment. Reads from stdin, writes 1 or 0 to stdout. Abuses ints as pointers, leaks memory, and uses the dreaded gets :)


In case you're curious, here's the opposite combinations of quine and IO methods:

C (gcc), 136 bytes

x;main(y,v)int**v;{asprintf(&x,y="x;main(y,v)int**v;{asprintf(&x,y=%c%s%1$c,34,y);return!strcmp(x,v[1]);}",34,y);return!strcmp(x,v[1]);}

Try it online!

C (gcc), 158 bytes

#define Q(X)v[40];main(){read(0,v,159);putchar(48+!strcmp(#X"\nQ("#X")",v));}
Q(#define Q(X)v[40];main(){read(0,v,159);putchar(48+!strcmp(#X"\nQ("#X")",v));})

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 38 bitsv2, 4.75 bytes

`I=`I=

Try it Online!

Explained

`I=`I=­⁡​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌­
     =  # ‎⁡Does the input equal
`I=`    # ‎⁢The string "I="
    I   # ‎⁣With itself wrapped in backticks prepended
💎

Created with the help of Luminespire.

\$\endgroup\$
1
\$\begingroup\$

Uiua, 38 bytes

≅&sc⊂∶!(⊂⊂↷.+1@!)."≅&sc⊂:!(⊂⊂↷.+1@!)."

Explained

≅&sc⊂∶!(⊂⊂↷.+1@!)."≅&sc⊂:!(⊂⊂↷.+1@!)."­⁡​‎‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁤‏⁠‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏⁠‎⁡⁠⁢⁣⁣‏⁠‎⁡⁠⁢⁣⁤‏⁠‎⁡⁠⁢⁤⁡‏⁠‎⁡⁠⁢⁤⁢‏⁠‎⁡⁠⁢⁤⁣‏⁠‎⁡⁠⁢⁤⁤‏⁠‎⁡⁠⁣⁡⁡‏⁠‎⁡⁠⁣⁡⁢‏⁠‎⁡⁠⁣⁡⁣‏⁠‎⁡⁠⁣⁡⁤‏⁠‎⁡⁠⁣⁢⁡‏⁠‎⁡⁠⁣⁢⁢‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁢⁡⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁢⁡⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁣​‎‏​⁢⁠⁡‌⁢⁤​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌­
                  "≅&sc⊂:!(⊂⊂↷.+1@!)."  # ‎⁡The string "≅&sc⊂:!(⊂⊂↷.+1@!)."
                 .                      # ‎⁢Duplicate it
      !(        )                       # ‎⁣Call the following function on the top copy:
           .+1@!                        # ‎⁤  Push two "
         ⊂↷                             # ‎⁢⁡  Append the first
        ⊂                               # ‎⁢⁢  Prepend the second
# ‎⁢⁣The top of the stack is now a quotified string
    ⊂∶                                  # ‎⁢⁤Prepend the original string without quotes
≅&sc                                    # ‎⁣⁡Does that equal the input?
💎

Created with the help of Luminespire.

\$\endgroup\$
0
\$\begingroup\$

V, 19 bytes

ñOÑ~"qpxØ¥^¨©î±¥$

Try it online!

Prints 1 or 0. Here is a hexdump:

00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee  .O..~"qpx..^....
00000010: b1a5 24                                  ..$
\$\endgroup\$
0
\$\begingroup\$

Java 8, 288 bytes (Full Program with STDIN)

interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}";System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}

Try it online.

Java 8, 210 bytes (Full Program with Console Arguments)

interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(a[0]));}}";System.out.print(s.format(s,34,s).equals(a[0]));}}

Try it online.

Java 8, 108 bytes (Function)

i->{String s="i->{String s=%c%s%1$c;return s.format(s,34,s).equals(i);}";return s.format(s,34,s).equals(i);}

Try it online.

Explanation:

-part:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

Challenge-part:

  • .equals(...) checks if this formatted source code equals the input.
    • java.util.Scanner(System.in).nextLine() is used as this input for STDIN
    • a[0] is used as this input for Console arguments
    • i is used as this input for the lambda function
\$\endgroup\$
0
\$\begingroup\$

Pascal (FPC), 167 157 bytes

const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.';a='const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.

Try it online!

While substring can be easily extracted, like in my regular quine, unfortunately, they cannot be concatenated because FPC thinks it is an array in these circumstances. In a and b are characters before and after constant definitions. #39#59#97#61#39 is a replacement for ';a=' as this is not represented in the constants. #39 represents ' and is at the start of b, glued together to the rest of b, to shorten the comparison expression as much as possible.

\$\endgroup\$
0
\$\begingroup\$

TeX, 55 bytes

The file has to be saved as a.tex and should be run using pdftex a.tex. The script doesn't terminate after returning 1 or 0, if that should be necessary append it by \end (+4 bytes). If it should work for arbitrary file names replace \openin0a with \openin0\jobname (+7 bytes).

\openin0=a\read0to\0\read1to~\message{\ifx\0~1\else0\fi}
\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), 42 bytes

exec(s:="print('exec(s:=%r)'%s==input())")

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java, 318 bytes

interface Q{static void main(String[]a){char q=34;String s="interface Q{static void main(String[]a){char q=34;String s=%c%s%c;System.out.print(new java.util.Scanner(System.in).nextLine().equals(String.format(s,q,s,q)));}}";System.out.print(new java.util.Scanner(System.in).nextLine().equals(String.format(s,q,s,q)));}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 11 bytes

¶BîQi"¶BîQi

Try it

¶BîQi"¶BîQi     :Implicit input of string
¶               :Is strictly equal to
 B              :  11
  î             :  Mold to length B
   Q            :    Quotation mark
    i           :    Prepend
     "¶BîQi     :      Literal string
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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