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
49 Answers 49
Runic, 11 bytes
"3X4+kSqi=@
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
-
1
-
\$\begingroup\$ @JoKing Very clever. \$\endgroup\$Draco18s no longer trusts SE– Draco18s no longer trusts SE2018年09月30日 15:00:26 +00:00Commented Sep 30, 2018 at 15:00
-
-
\$\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\$Draco18s no longer trusts SE– Draco18s no longer trusts SE2018年09月30日 15:40:24 +00:00Commented Sep 30, 2018 at 15:40
05AB1E, 15 bytes
0"D34çýQ"D34çýQ
Modifies the default quine 0"D34çý"D34çý by adding Q (check for equality with the implicit input)
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
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)
-
3\$\begingroup\$
187745012D27BJQis a tie. \$\endgroup\$Grimmy– Grimmy2019年06月13日 12:21:57 +00:00Commented Jun 13, 2019 at 12:21
Perl 6, 33 bytes
<dd "<$_>~~.EVAL"eq slurp>~~.EVAL
Outputs either Bool::True or Bool::False to STDERR. This is the basic quine format, with an added check against input (eq slurp).
Keg, (削除) 36 (削除ここまで) (削除) 28 (削除ここまで) 26 bytes
`:&\`n^\`n&∑=`:&\`n^\`n&∑=
Same concept as before, but shorter.
Answer History
`:&\`n⅍^\`n⅍++&+¿=`:&\`n⅍^\`n⅍++&+¿=
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.
-
\$\begingroup\$ It still outputs
1if the input is followed by a newline and then anything else \$\endgroup\$Jo King– Jo King2020年01月19日 23:32:43 +00:00Commented 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\$2020年01月19日 23:38:27 +00:00Commented Jan 19, 2020 at 23:38
Japt, (削除) 22 (削除ここまで) (削除) 16 (削除ここまで) 14 bytes
"iQ 2¶U"iQ 2¶U
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.
#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
Ruby -0n, (削除) 32 (削除ここまで) 27 bytes
Saved 5 bytes thanks to a comment by @Sisyphus on another answer.
eval s="p$_=='eval s=%p'%s"
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.
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]);})
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));}
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]);}
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));})
Vyxal, 38 bitsv2 , 4.75 bytes
`I=`I=
Explained
`I=`I=
= # Does the input equal
`I=` # The string "I="
I # With itself wrapped in backticks prepended
💎
Created with the help of Luminespire.
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.
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 . Improved this by open Trim (削除ここまで)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
-
\$\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\$2024年12月12日 13:05:41 +00:00Commented Dec 12, 2024 at 13:05
-
\$\begingroup\$ Sure. Just changed to the case sensitive version, which is 2 bytes longer :) \$\endgroup\$amorphobia– amorphobia2024年12月12日 13:22:44 +00:00Commented Dec 12, 2024 at 13:22
V, 19 bytes
ñOÑ~"qpxØ\^ ̈©î±\$
Prints 1 or 0. Here is a hexdump:
00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee .O..~"qpx..^....
00000010: b1a5 24 ..$
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()));}}
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]));}}
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);}
Explanation:
quine-part:
- The
String scontains the unformatted source code. %sis used to input this String into itself with thes.format(...).%c,%1$cand the34are 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 STDINa[0]is used as this input for Console argumentsiis used as this input for the lambda function
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.
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.
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}
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)));}}
true/falseoutput a strong requirement, or are variations (True/False,1/0) acceptable as well? \$\endgroup\$