Friend, or Foe?

Provide two programs/functions A and B (not necessarily in the same language) with the following properties:

• Either program, when given its own source code as input, produces an accepting output.
• Either program, when given the other program's source code as input, produces a rejecting output.

It doesn't matter what happens when you provide either program with an input that is not A or B. Accepting or rejecting is defined as per here:

• output truthy/falsy using your language's convention (swapping is allowed), or
• use two distinct, fixed values to represent true (affirmative) or false (negative) respectively.

Both programs must use the same method of output. Additionally, if you use truthy/falsy and they are swapped for one program, they must be swapped for both programs.

If you decide to write a function instead of a full program, you must do so for both programs.

Scoring

Your score is the sum of the byte counts of both programs. This is code golf, so the lowest score wins.

• Is program to be taken literally? Or are functions allowed? Dec 26, 2022 at 22:52
• @Arnauld They could be functions too. I'll add that. Dec 27, 2022 at 2:08

Jelly, 1 byte

Program A, 1 byte
Ṇ

Outputs

Falsy = accepting; truthy = rejecting

Explanation
• The string Ṇ (program A) is truthy as it is a non-empty string
• The string   (program B) is falsy as it is an empty string
• In Jelly, an empty program just outputs exactly what was input. So program B produces a truthy output when program A is inputted, and a falsy output when it is given itself as input
• In Jelly, Ṇ is the logical NOT operator. It outputs a falsy output (0) when a truthy input is given, but a truthy output (1) when a falsy input is given. Therefore, it produces a falsy output when given itself as input, and a truthy output when program B is inputted

APL (Dyalog Unicode), 107 6 bytes

Anonymous tacit prefix functions, returning ⊣ for true and ⊢ for false.

A: ⊣/⊢

⊢ is an identity function, and returns the given source as-is

⊣/ returns the leftmost character (lit. left-identity reduction), i.e. ⊣ when given A's source and ⊢ when given B's source.

B: ⊢/⊣

⊣ is an identity function, and returns the given source as-is

⊢/ returns the rightmost character (lit. right-identity reduction), i.e. ⊢ when given A's source and ⊣ when given B's source.

Try it online!

• I like the symmetry of this solution Dec 27, 2022 at 6:38

Retina 0.8.2, 2 bytes

Program A:

A


Program B:

B


Explanation: Each program counts the number of As or Bs in the input respectively, which will be truthy for itself and falsy for the other program.

Pip, 6 5 bytes

• -1 thanks to DLosc

Outputs 0 for truthy and 1 for falsy.

Program A, 2 bytes

+q


Program B, 3 bytes

1-q


Explanation

+q forces the input into an integer. +q evaluates to 0, and 1-q evaluates to 1.

1-q subtracts the input from 1, once again forcing it into an integer. +q will therefore be 1, and 1-q will therefore be 0.

• @DLosc thanks, updated. Dec 30, 2022 at 8:35

R, 26 24 bytes

Program A, 12 bytes

\(x)x<"\\(y"


Attempt This Online!

Program B, 12 bytes

\(z)z>"\\(y"


Attempt This Online!

Two anonymous functions differing by the name of the variable. We then check whether the function text (essentially the variable name) is smaller or bigger (lexicographically) than the beginning of the function (\ escaped and () with y appended.

• Can you explain this one? I'm not that type of person that is familiar with R. Dec 28, 2022 at 12:44
• @Joao-3 here you go. Dec 28, 2022 at 12:51
• @pajonk - I saw your grep version on mobile, and thought that something like this could be shorter... and now when I got to a computer I found you'd beaten me to it! ...but... now found a 23-byter... Dec 30, 2022 at 16:42

Python 2, 16 + 15 = 49464440 31 bytes

crossed out 44 is still regular 44 :(

I don't think there's a optimal way to golf this.

-1 for Truthy, 0 for Falsy

Program A

0
print~-input()


Input A

Input B

Program B

1
print-input()


Input A

Input B

-2 bytes thanks to Jiři

-9 thanks to tsh by changing the version.

• I think you can save 2 bytes by using positive indexes.
– Jiří
Dec 26, 2022 at 22:20
• 6\nprint(input()%5) and 5\nprint(input()%2) for python 2.
– tsh
Dec 27, 2022 at 2:14
• @tsh Thanks, but I golfed your code(s) by 5 bytes. How did I not think about that??? Dec 27, 2022 at 3:23

Vyxal, 4 bytes

Program A, 2 bytes

0c


Program B, 2 bytes

1c


Explanation

0c checks whether 0 is in the input, and 1c checks whether 1 is in the input.

JavaScript (ES6), 2x7 = 14 bytes

Both functions return a Boolean value.

A: s=>s>{}

B: S=>S<{}

Try it online!

Zsh, 6 + 6 = 12 bytes

grep A


Attempt This Online!

grep B


Attempt This Online!

Output is via exit code.

><>, 10 bytes

0"i=n;


And

1io;


Try it

Explanation

First we push a 0 or a 1. This is useless, but identifies the programs.

Then we push the string "0=n;0" (because strings wrap around). This means the 0 or 1 character code will be the last character on the stack.

We get the first character of the output program, and print if they are equal we print 1, otherwise 0.

The second program is similar except we can just print the first character of the first program so we can skip the = to save 1 byte.

Motorola MC14500B Machine Code, 3 bytes

Outputs 0 for truthy and 1 for falsy.

Program A, 1.5 bytes

118


Disassembled:

1    ; Read input from the I/O pin into RR.
1    ; Read input from the I/O pin into RR.
8    ; Output RR to the I/O pin.


Program B, 1.5 bytes

428


Disassembled:

4    ; Read input from the I/O pin and store (RR AND (NOT INPUT)) into RR.
2    ; Read input from the I/O pin and store (NOT INPUT) into RR.
8    ; Output RR to the i/O pin.


Explanation

Program A outputs the second bit of input, which is 0 for Program A and 1 for Program B.

Program B outputs the complement of the second bit of input, which is 1 for Program A and 0 for Program B.

function A, 11 bytes

even.length

function B, 10 bytes

odd.length

Brachylog, 2 bytes

0


Try it online!

1


Try it online!

With inspiration from Neil's Retina answer. Both full programs succeed on themselves and fail on each other. Each program unifies the input (and output) with the single numeric literal in the body, which matches the syntax for providing numbers as input.

Brainbool, 6 bytes

A: 1,.
B: ,+.

A explanation:

1 this is comment used for "1" input
. and print it back


B explanation:

, reads character; also it is used as "0" input
+ negates the value
. print the value


MATL (both programs), score 4

Program A: oo

Program B: I\

Each program with itself as input outputs a truthy result, specifically a numeric array containing positive integers. Each program with the other as input outputs a falsy result, specifically a numeric arrray containing at least a zero entry. See truthy/falsy criteria in MATL, or use this truthy/falsy test.

How it works

Program A:

o  % Implicit input. Convert codepoints to numbers. Gives numeric vector
o  % Modulo 2 of each entry. Implicit display


Program B:

I  % Push 3
\  % Implicit input. Modulo (3, of code points). Gives numeric vector. Implicit display


Desmos, 34 bytes

Program A, 17 bytes


A(l)=\{l[2]=65\}


Program B, 17 bytes


B(l)=\{l[2]=66\}


Input is a list of character codepoints, and output is 1 for truthy (accepting) and undefined for falsey (rejecting).

This is just the first strategy I thought of; there might be better ways out there.

Try It On Desmos!

Try It On Desmos! - Prettified

brainfuck, 7 bytes

Outputs , for truthy and F for falsy.

Program A, 3 bytes

,F.


Program B, 4 bytes

F,,.


Explanation

Program A retrieves and prints the first byte of input, and Program B retrieves and prints the second byte of input. An F char (no-op) is inserted into each program in the opposite position of which the program prints out (so that Program A's F is the second char and Program B's F is the first char).

Java 19 using exit codes, 146 (73 + 73)

interface A{static void main(String[]a){System.exit(a[1].charAt(0)%65);}}

interface B{static void main(String[]a){System.exit(a[1].charAt(0)%66);}}


Run it with variations of:

cat A.java | xargs java B
echo $?  Java 19 using standard out, 158 (79 + 79) bytes interface A{static void main(String[]a){System.out.print(a[1].charAt(0)==65);}}  interface B{static void main(String[]a){System.out.print(a[1].charAt(0)==66);}}  Run it with variations of: cat A.java | xargs java A  • Wouldn't using class instead of interface save some bytes? Dec 28, 2022 at 12:18 • Using class instead of interface saves 8 bytes. Dec 28, 2022 at 12:39 • If you use class instead of interface, then you need to be explicit about making the main method public. This isn't true for interfaces because all methods on interfaces are public by default. That is, class A{public is 2 characters more than interface A{. Dec 28, 2022 at 12:55 05AB1E, 6 4 bytes • -2 thanks to Kevin Cruijssen Program A, 3 2 bytes 0å  Program B, 3 2 bytes 1å  Test cases Explanation 0å checks whether 0 is in the input, and 1å checks whether 1 is in the input. • No need for the . in either program. .å is a non-vectorized contains check (which I haven't used even once thus far I think 🤔). Just å does what you want. Dec 28, 2022 at 14:22 • @KevinCruijssen thanks Dec 28, 2022 at 15:19 R, 11+12 = 23 bytes prog a (11 bytes) scan(,'')>F  prog a with itself as input = TRUE prog a with prog b as input = FALSE prog b (12 bytes) !scan(,'')>F  prog a with prog a as input = FALSE prog a with itself as input = TRUE x86_32 machine code, 6 bytes Takes input in ESI (interpreted as a uint8_t *), and outputs to AL 0xAC for truthy and 0x46 for falsy. Try it online! Function A, 3 bytes 08049001 <a>: 8049001: ac lodsb 8049002: 46 inc esi 8049003: c3 ret  Function B, 3 bytes 08049005 <b>: 8049005: 46 inc esi 8049006: ac lodsb 8049007: c3 ret  Explanation Function A copies the first byte of input to AL with lodsb. Function B copies the second byte of input to AL by first incrementing ESI before executing lodsb. When Function A is run with itself or Function B is run with itself, the first/second byte (respectively) is copied to AL, which in both cases ends up being 0xAC. When Function A is run with B or Function B is run with A, the opposite byte (0x46) is copied to AL. Thue, 39 + 39 bytes a::=b b::=~1 c::=::: A::=d d::=~0 ::= c  A::=B B::=~1 C::=::: a::=D D::=~0 ::= C  Has an additional newline (actually two) because of TIO implementation issue. Truthy = 11 or 1\n1 depending on the implementation, in this case, 1\n1\n Falsy = 0 POSIX Shell + Utilities, 8+9=17 bytes $ cat ff?; echo; wc -c ff?
cmp $0 - cmp$0 -
9 ff1
8 ff2
17 total
$./ff1 < ff1; echo$?
0
$./ff1 < ff2; echo$?
1
$./ff2 < ff1; echo$?
1
$./ff2 < ff2; echo$?
0


Some cmp implementations would allow dropping the  -, for a score of 6+7=13 bytes.

Charcoal, 6 bytes

Program A:

№ＳA


Program B:

№ＳB


Explanation: Each program counts the number of As or Bs in the input respectively, which will be truthy for itself and falsy for the other program.

sed, 35 bytes

Program A, 17 bytes

s/s.*/1/;s/;.*/0/


Program B, 18 bytes

;s/s.*/0/;s/;.*/1/


Explanation

Program B starts with ; to distinguish it from Program A.

Program A replaces lines starting with s with 1, and replaces lines starting with ; with 0.

Program B replaces lines starting with s with 0, and replaces lines starting with ; with 1.

gbz80machine code functions, 8 bytes

(Registers and flags will be referred to in lowercase letters, and the functions in uppercase letters.)

These functions require that the function to test is pointed to by hl. Their truthiness is output in the z flag: true is a match, false is a non-match.

Function A, 4 bytes:

3e 3e be c9


Disassembly:

ld a, \$3e ; 3e is the opcode of "ld a, d8"
cp [hl]
ret


Function B, 4 bytes:

00 af be c9


Disassembly:

nop
xor a ; set a to 0
cp [hl]
ret


We're just checking the first byte of the function using two different ways to set a to a constant.

PUBERTY, 241 bytes

Outputs 0 for truthy and I for falsy.

Program A, 118 bytes

0
It is May 1, 1000, :: PM.q is in his bed, bored.His secret kink is t.Soon the following sounds become audible.oh yes


Program B, 123 bytes

I
0
It is May 1, 1000, :: PM.q is in his bed, bored.His secret kink is t.Soon the following sounds become audible.oh oh yes


Explanation

Both start with no-ops for simple processing. This answer works in the same way as my brainfuck answer. Program A prints out the first char of input (not including newlines, which are required for the input to be registered) and Program B prints out the second.

GNU sed, 10 bytes

Program A: 5 bytes

/a/q1


Program B: 5 bytes

/b/q1


Run with sed -nf <program_filename> <input_filename> Both programs output via their exit code, where 0 is accepting (or "friend") and 1 is rejecting (or "foe").

By default, sed outputs with exit code 0. However, the q command in GNU sed can terminate with a different exit code. Program A searches the input text for the letter a (which exists in Program A but not Program B). If it's present, then we terminate early with exit code 1. If not, we terminate at the end with the default exit code of 0. Program B does the exact same thing, except that it searches for letter b instead of a.

TI-Basic, 8 bytes

Program A, 5 bytes

6-length(Ans


Program B, 3 bytes

length(Ans


Both programs take input from Ans as a string. Both output 2 for truthy and 4 for falsy.

Thunno, $$\1\log_{256}(96)\approx\$$ 0.82 bytes

Port of Lecdi's Jelly answer. Falsy = accepting; Truthy = rejecting.

Program A, $$\1\log_{256}(96)\approx\$$ 0.82 bytes

!


Explanation

! is the operator for logical not.