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
DJMcMayhem
60.1k18 gold badges203 silver badges352 bronze badges
asked Apr 17, 2013 at 14:29
\$\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\$ Commented Jan 20, 2020 at 7:37

49 Answers 49

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
answered Sep 30, 2018 at 3:04
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 10 bytes \$\endgroup\$ 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\$ 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)
answered Sep 7, 2018 at 13:41
\$\endgroup\$
1
  • 3
    \$\begingroup\$ 187745012D27BJQ is a tie. \$\endgroup\$ 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).

answered Jan 19, 2020 at 23:47
\$\endgroup\$
1
\$\begingroup\$

Keg, (削除) 36 (削除ここまで) (削除) 28 (削除ここまで) 26 bytes

`:&\`n^\`n&∑=`:&\`n^\`n&∑=

Try it online!

Same concept as before, but shorter.

Answer History

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

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:

`\`n⅍^\`n⅍++`

Which would consequently be followed by:

 \`n⅍^\`n⅍++

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:

`\`n⅍^\`n⅍++¿=`\`n⅍^\`n⅍++¿=

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:

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

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

answered Jan 19, 2020 at 23:30
\$\endgroup\$
2
  • \$\begingroup\$ It still outputs 1 if the input is followed by a newline and then anything else \$\endgroup\$ 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\$ Commented Jan 19, 2020 at 23:38
1
\$\begingroup\$

Japt, (削除) 22 (削除ここまで) (削除) 16 (削除ここまで) 14 bytes

"iQ 2¶U"iQ 2¶U

Try it online!

Explanation

"iQ 2¶U"iQ 2¶U
"iQ 2¶U" // Take this string. iQ 2¶U
 iQ // Insert a quote. "iQ 2¶U
 2 // Double. "iQ 2¶U"iQ 2¶U
 ¶U // Check if equal to input.
answered Jun 29, 2020 at 7:57
\$\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
answered Jan 12, 2016 at 14:21
\$\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.

answered Dec 22, 2020 at 23:24
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 48 bytes

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

Try it online!

answered Jun 3, 2023 at 13:39
\$\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!

answered Jun 3, 2023 at 4:04
\$\endgroup\$
1
\$\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
answered Jun 5, 2023 at 23:33
\$\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.

answered Sep 27, 2023 at 11:42
\$\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.

answered Sep 30, 2023 at 12:38
\$\endgroup\$
1
\$\begingroup\$

AutoHotkey (v2), input as arguments, output to MsgBox, (削除) 91 (削除ここまで) (削除) 93 (削除ここまで) (削除) 87 (削除ここまで) 81 bytes

(削除) Case insensitive comparison. It's OK since function & var names and strings are all case insensitive (削除ここまで)

Let it be as strict as possible. Change to a case sensitive comparison.

MsgBox Format(s:='MsgBox Format(s:={:c}{}{1:c},39,s)==A_Args[1]',39,s)==A_Args[1]

It takes command line arguments as input (which need to be double quoted) and outputs in MsgBox.

AutoHotkey (v2), input from stdin1, output to MsgBox, (削除) 194 (削除ここまで) (削除) 167 (削除ここまで) 122 bytes

MsgBox Format(s:="MsgBox Format(s:={:c}{}{1:c},34,s,10)==FileOpen('*','4').Read(){3:c}",34,s,10)==FileOpen('*','4').Read()

It turns out that when reading from stdin, AHK will add extra new lines, (削除) and it has to be removed with Trim (削除ここまで). Improved this by open STDIN with mode 4, which converts CR LF to LF. Then append LF to the comparing string.

AutoHotkey (v2), input from stdin, output to stdout1, (削除) 212 (削除ここまで) (削除) 183 (削除ここまで) 138 bytes

FileAppend Format(s:="FileAppend Format(s:={:c}{}{1:c},34,s,10)==FileOpen('*','4').Read(),'*'{3:c}",34,s,10)==FileOpen('*','4').Read(),'*'

1: AutoHotkey scripts usually run as GUI app on Windows, so stdin/stdout need to be "piped" to/from the program. Also, to literally interpret all quotes as-is, single-quoted "here-strings" must be used as input:

echo @'
input 'string' with "quotes" """ `` '
'@ | autohotkey.exe script.ahk | echo
answered Dec 12, 2024 at 9:18
\$\endgroup\$
2
  • \$\begingroup\$ I don't think the case insensitive version is valid. It's ok to include it but I think you should put the valid version up front. \$\endgroup\$ Commented Dec 12, 2024 at 13:05
  • \$\begingroup\$ Sure. Just changed to the case sensitive version, which is 2 bytes longer :) \$\endgroup\$ Commented Dec 12, 2024 at 13:22
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 ..$
answered Jul 14, 2017 at 20:07
\$\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
answered Jan 29, 2018 at 12:50
\$\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.

answered Oct 2, 2018 at 15:15
\$\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\read0to0円\read1to~\message{\ifx0円~1\else0\fi}
answered Sep 9, 2019 at 13:55
\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), 42 bytes

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

Try it online!

answered Jan 28, 2020 at 6:21
\$\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!

answered Feb 25, 2021 at 2:05
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.