You're tasked with writing two programs. Program A must print out nothing on all inputs except when program B is input, in which case it should print 1. Program B must print out 1 on all inputs except when program A is input, in which case it should print nothing.


  • +1 For each character from both programs.
  • Lowest score wins.
  • 3
    \$\begingroup\$ Is this quine-ish enough to tag it as quine? It certainly seems so to me. \$\endgroup\$ – Justin Jan 9 '14 at 20:26
  • \$\begingroup\$ As the first two answers posted indicate, "being" another program is not terribly well defined in this descriptions. And, I'm with @Quincunx that this has something very much like the quine-nature. \$\endgroup\$ – dmckee --- ex-moderator kitten Jan 9 '14 at 22:25
  • \$\begingroup\$ @Quincunx I've added the quine tag. \$\endgroup\$ – Timtech Jan 9 '14 at 23:45
  • 1
    \$\begingroup\$ @Quincunx true, but nobody is doing that so far, unless you also call a quine any program that reads its source code from disk and prints it out :p \$\endgroup\$ – aditsu quit because SE is EVIL Jan 10 '14 at 4:59
  • 2
    \$\begingroup\$ @aditsu I much dislike those answers. I think I'll post a very suboptimal answer that does not do that. I personally feel that reading source code through files is cheating; programs should work anywhere! \$\endgroup\$ – Justin Jan 10 '14 at 5:11

12 Answers 12


GTB, 25

Executed from a TI-84 calculator

Program A


Program B



`_ Input a string

@_eq;" Check if it equals the source code (# is automatically stripped along with lowercase letters)

$w;& If so, display 1 (otherwise nothing) [for B it's $#w;& - if not, display 1 (otherwise nothing)]


Bash - 32 characters

Script A - 16 characters

cmp -s b&&echo 1

Script B - 16 characters

cmp -s a||echo 1


$> echo "foo" | ./a
$> cat b | ./a
$> echo "foo" ./b
foo ./b
$> cat a | ./b

Ruby, 54






bash-3.2$ ruby A < A
bash-3.2$ ruby A < B
1bash-3.2$ ruby B < A
bash-3.2$ ruby B < B

J (62)

Since you didn't forbid this...

Store the programs as A and B respectively.

Program A (30):

exit echo#~(1!:1<'B')-:1!:1[3

Program B (32):

exit echo#~-.(1!:1<'A')-:1!:1[3

How it works (Program B, A is similar):

  • 1!:1[3: read stdin
  • 1!:1<'A': read file A
  • -:: see if they are equal
  • -.: negate the result
  • #~: replicate the result by itself (so, 1 results in one 1 and 0 results in zero 0s, i.e. nothing)
  • echo: output
  • exit: exit (the J interpreter does not exit by default when it reaches the end of the file)
$ jconsole A <B
$ jconsole A <foo
$ jconsole B <A
$ jconsole B <foo
  • \$\begingroup\$ Can you give a short explanation of what this does? \$\endgroup\$ – ike Jan 9 '14 at 20:39
  • \$\begingroup\$ @ike: did so _______ \$\endgroup\$ – marinus Jan 9 '14 at 20:43

Haskell - WITHOUT loading source - 478 644 characters

This assumes getContents ALWAYS ends with a newline and so drops the final character without checking because I don't feel like escaping it


main=interact$($'1').replicate.(1-).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='-'='*'|n=='*'='-'|True=n;d="main=interact$($'1').replicate.(1-).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='-'='*'|n=='*'='-'|True=n;d="


main=interact$($'1').replicate.(1*).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='*'='-'|n=='-'='*'|True=n;d="main=interact$($'1').replicate.(1*).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='*'='-'|n=='-'='*'|True=n;d="

It works like a standard quine, but swapping - for * to get the other program (avoiding those characters elsewhere).

The following test prints as expected (replacing main=interact$ with a= and b=)

  putStrLn "START"
  putStrLn$a "FOO"
  putStrLn$a "main=interact$($'1').replicate.(1*).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='*'='-'|n=='-'='*'|True=n;d=\"main=interact$($'1').replicate.(1*).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='*'='-'|n=='-'='*'|True=n;d=\"\n"
  putStrLn$b "FOO"
  putStrLn$b "main=interact$($'1').replicate.(1-).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='-'='*'|n=='*'='-'|True=n;d=\"main=interact$($'1').replicate.(1-).fromEnum.(/=map r(d++shows d[toEnum 10]))where r n|n=='-'='*'|n=='*'='-'|True=n;d=\"\n"
  putStrLn "END"





Python 2.7 - 82

File A (literally named just a):

if raw_input()==open('b').read():print 1

File B (literally named just b):

if raw_input()!=open('a').read():print 1
  • \$\begingroup\$ Total abuse there without the .py - does that even work? \$\endgroup\$ – Timtech Jan 9 '14 at 23:41
  • \$\begingroup\$ I'm sure @LegoStormtroopr would be running in the same manner as my Ruby examples posted here, a few minutes earlier. ;-) \$\endgroup\$ – Darren Stone Jan 9 '14 at 23:55
  • 1
    \$\begingroup\$ @Timtech It does if you run them from the command line as python a. \$\endgroup\$ – user8777 Jan 10 '14 at 0:17
  • \$\begingroup\$ I meant is it possible to even generate a file without an extension? \$\endgroup\$ – Timtech Jan 10 '14 at 0:21
  • 5
    \$\begingroup\$ Of course it is? If you are on a Posix machine touch a will create an empty file if you have permissions. For extra cruel fun you can even do touch \~ which creates a file named with a single tilde (~) - then watch as someone foolhardedly tries to remove it ;) \$\endgroup\$ – user8777 Jan 10 '14 at 0:35

Ruby, 166 chars, no reading source





Make sure your text editor doesn't save with a trailing newline.

Usage (example):

 $ ruby know_a.rb know_b.rb 
 $ ruby know_a.rb know_a.rb 
 $ ruby know_b.rb know_a.rb 
 $ ruby know_b.rb know_b.rb 

Each program constructs the source of the other program using a HEREdoc and string transforms, then compares the result to the input.

  • \$\begingroup\$ This was easy enough to write, but now some part of my brain that doesn't understand recursion is insisting it can be optimized but has no idea how. \$\endgroup\$ – histocrat Jan 10 '14 at 20:05
  • \$\begingroup\$ What is p? And where does the heredoc end? \$\endgroup\$ – aditsu quit because SE is EVIL Jan 10 '14 at 21:32
  • \$\begingroup\$ p is a built-in ruby method that prints the arguments passed to it, then returns those arguments, making it useful for golfed output. When called with no arguments it returns nil. The argument to gets is a delimiter, so passing p results in a nil delimiter which means it reads STDIN until it gets to EOF. The heredoc expression is <<2, so it ends at (and does not include), the 2 at the end of the file. \$\endgroup\$ – histocrat Jan 10 '14 at 21:40
  • \$\begingroup\$ The use of 2 as the heredoc delimiter is a traditional bit of obfuscation. It can be almost any string. \$\endgroup\$ – histocrat Jan 10 '14 at 21:43
  • \$\begingroup\$ Is the content of the heredoc evaluated somehow? \$\endgroup\$ – aditsu quit because SE is EVIL Jan 10 '14 at 21:49

Haskell - 138

Not really a good answer, but wanted to make both programs use the same source. Could save some chars by renaming the file, but it's not going to make this a winning solution so I don't think it's worth it.

import System.Environment
import Control.Monad
main=do{i<-getContents;p<-getProgName;f<-readFile "ab.hs";when((f==i)/=(p=="B"))(print 1)}

Compile this source as both A and B.


% ghc -o A ab.hs
[1 of 1] Compiling Main             ( ab.hs, ab.o )
Linking A ...
% cp A B
% ./A < ab.hs
% ./B < ab.hs
% ./A < ab.hi
% ./B < ab.hi
  • \$\begingroup\$ why compile twice into A, and then copy A to B? \$\endgroup\$ – mniip Jan 9 '14 at 22:45
  • \$\begingroup\$ That was a mistake by my part when copypasting the code. Thanks for pointing that out. Will fix. \$\endgroup\$ – shiona Jan 9 '14 at 22:46

Node.js - 142 characters

Script | (otherwise known as Script A) - 80 characters


Script & (otherwise known as Script B) - 62 characters



# \| is Script A
# \& is Script B

$> echo "foo" | node \| 
$> cat \& | node \| 
$> echo "foo" | node \& 
$> cat \| | node \&


Script B reads the contents of Script A and evals it after swapping the file names and the and operator to an or.

I named the files & and | so I can perform a single replace in Script B.


Python 3 - 102 characters

Prints 1 if the input is the same as program 2, otherwise nothing:

if input()==open('a.py').read():print('1')

Prints 1 if input is not the same as program 1, otherwise nothing:

if input()==open('a.py').read():print('1')
  • \$\begingroup\$ Can't the whitespace be removed? Also you can shorten the scripts from t.py and tt.py to a.py and b.py. \$\endgroup\$ – Timtech Jan 10 '14 at 1:28
  • \$\begingroup\$ @Timtech Sure, good idea. Also, I wasn't counting white-space - that is just there for readability. The new-lines cannot be removed though. \$\endgroup\$ – user10766 Jan 10 '14 at 1:40
  • \$\begingroup\$ Yes, I am aware of Python's newline sensitivity. \$\endgroup\$ – Timtech Jan 10 '14 at 2:33
  • \$\begingroup\$ Only one of the newlines can really be removed, that after the colon. The others would need semicolons added, so there is no advantage to removing those newlines. \$\endgroup\$ – AJMansfield Jan 10 '14 at 2:39
  • \$\begingroup\$ @AJMansfield Yes, I know, but I didn't count newlines anyway. \$\endgroup\$ – user10766 Jan 10 '14 at 2:40

bash/grep — 59 chars

51 chars if we only count the actual program string.

$ a='grep -cx "$b" | grep -x 1'
$ b='grep -vcx "$a" | grep -x 1'
$ echo 'foo' | eval $a
$ echo $b | eval $a
$ echo 'foo' | eval $b
$ echo $a | eval $b

R (62 chars)



> A(123)
> A(A)
> A(B)
[1] 1
> B(123)
[1] 1
> B(A)
> B(B)
[1] 1

Meta comment: R fairs relatively bad on code golf as there is no shortcut to function...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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