18
\$\begingroup\$

Task

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.

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

37 Answers 37

1
2
0
\$\begingroup\$

Jelly, 8 bytes

Program A, 4 bytes

0ŒṘe

Program B, 4 bytes

1ŒṘe

Test cases

Explanation

0ŒṘe checks whether 0 is in the input, and 1ŒṘe checks whether 1 is in the input.

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

Python 3, 36 bytes

print(''<input())

Try it online!


print(''==input())

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Do you need two programs? \$\endgroup\$
    – Fmbalbuena
    Dec 27, 2022 at 21:02
  • \$\begingroup\$ @Fmbalbuena What? \$\endgroup\$
    – l4m2
    Dec 27, 2022 at 21:09
  • \$\begingroup\$ Rollbacked since it's printed \$\endgroup\$
    – l4m2
    Dec 27, 2022 at 21:15
0
\$\begingroup\$

SussyLang, 88 bytes

Outputs 0 for truthy and 3 for falsy.

Program A, 44 bytes

pensus
sus 112
sussyballs
imposter
sussybaka

Program B, 44 bytes

sus 115
pensus
sussyballs
imposter
sussybaka

Test cases

Explanation

Program A takes the first character and subtracts 112 ('p'); Program B takes 115 ('s') and subtracts the first character.

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

Python 3, 44 40 bytes

A, 20 bytes

print('['in input())

B, 20 bytes too

print(']'in input())

A, when input A

A, when input B

B, when input A

B, when input B

What I really like about this one, is that you can change 1 character and get the other program.

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

Pyt, 6 bytes

Program A

0
=

Try it online!

Program B

1
=

Try it online!

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

C (gcc) and Python 3, 31 + 21 = 52 28 + 21 = 49 bytes

*-2 bytes by following @ceilingcat's suggestion, and -1 more by using <'p' rather than =='m' in C.

C:

main(){exit(getchar()<'p');}

Try It Online!

If there's a difference between the first character of input and 'm', the program returns 0; otherwise it returns 1.

Python 3:

print(input()[0]>'m')

Try It Online

If the ascii code for the first character of input is more than the one for 'm' (which p is but m isn't), the program prints True; otherwise it prints False.

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

Python, 20 bytes

Program A, 10 bytes

"$".__gt__

Program B, 10 bytes

'$'.__lt__

Attempt This Online! Link includes all four test cases.

Function which takes a string, returning True for friend and False for foe.

How?

Python string comparison works character by character.

When comparing $ to "$".__gt__, only $ and " are compared.
Similarly, when comparing $ to '$'.__lt__, only $ and ' are compared.

In ASCII values, " < $ < '.

\$\endgroup\$
1
2

Your Answer

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

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