23
\$\begingroup\$

Submit a well-formed program or function with usual I/O rules which satisfy the input, output, and algorithm conditions below. Shortest submission (in bytes) wins. As always, standard loopholes are forbidden.

In short: Two distinct 8-bit integer inputs must produce two 8-bit integer outputs so that all four numbers are distinct, i.e., not equal.

Input Conditions

Two distinct 8-bit integers, a and b, both of which are in 8-bit signed (or unsigned at your discretion) format.

Output Conditions

Two 8-bit integers, c and d, which are in the same format as the input (signed or unsigned), such that a, b, c, and d are distinct, i.e., no two of them are equal to each other.

Algorithm Conditions

  1. The algorithm must halt for all possible valid inputs.
    • Valid inputs are those where a and b are distinct. When a and b are equal, the behaviour of the algorithm is unspecified.
  2. The algorithm must be deterministic.
    • Running the algorithm with the same input must produce the same output.
  3. The algorithm must satisfy the output conditions for all valid inputs.

Verification

There are two ways to verify your algorithm.

  1. Brute force: explicitly check the program for all valid input combinations.
  2. Prove/show, by analysis, that a clash is impossible (preferred).

FAQ

  1. What if my programming language doesn't provide 8-bit integers?
  2. What if my programming language doesn't support fixed-width integers?

    • Only the range of the outputs matter; internal computations may be in any precision. If the outputs can be rounded/scaled to the required integral range, then the programming language is good to go.

Edits

Edit 1: Thank you for the submissions! I am glad to see so many creative solutions! Initially, I planned to restrict the algorithm to one-to-one mapping (i.e., invertible), however, decided against it to make the problem simpler. Looking at the enthusiastic response, I will consider submitting a second code golf with the one-to-one mapping requirement.

\$\endgroup\$
  • \$\begingroup\$ I would suggest explicitly allowing the deterministic output to be unordered, e.g. an input can produce [1,2] or [2,1] or a set that contains 1 and 2. \$\endgroup\$ – Jo King Mar 23 at 14:20
  • \$\begingroup\$ Just to be sure, 0s have to be taken into account right? The input may have one 0? \$\endgroup\$ – RGS Mar 23 at 14:28
  • \$\begingroup\$ @JoKing sorry, without any disrespect, I do not think this is acceptable. The reason being that the original requirements of determinism are for output integers "c" and "d;" combining them into a set is only a convenience, which is allowed per the rules. \$\endgroup\$ – Aravindh Krishnamoorthy Mar 23 at 14:33
  • 1
    \$\begingroup\$ @RGS, yes, input range for 8-bit signed is [-128,127] and for 8-bit unsigned is [0,255]. \$\endgroup\$ – Aravindh Krishnamoorthy Mar 23 at 14:34
  • 3
    \$\begingroup\$ Similar to codegolf.stackexchange.com/questions/139606/the-third-string \$\endgroup\$ – my pronoun is monicareinstate Mar 23 at 15:46

21 Answers 21

8
\$\begingroup\$

JavaScript (Node.js), 35 31 27 23 bytes

-4 bytes thanks to Arnauld and Kevin Cruijssen

-4 bytes thanks to xnor

a=>b=>[c=3^a&1^b&2,c^4]

Try it online!

Explanation:

Takes the flipped 1 bit of the first number, and adds the flipped 2 bit of the second number, guaranteeing a number different from both inputs in the 2 least significant bits. Outputs this number, and 4 added to this number

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ If you change the + to |, it looks like you can cut out the inner parens. Or better: Try it online! \$\endgroup\$ – xnor Mar 23 at 20:00
5
\$\begingroup\$

Perl 6, 20 bytes

{grep(.none,^4)[^2]}

Try it online!

Outputs the first two numbers from the range 0 to 3 that aren't in the input.

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 9 5 bytes

∞IK2£

I/O as a pair of integers.

Try it online or verify some more test cases.

Explanation:

∞      # Push an infinite positive list [1,2,3,...]
 IK    # Remove the values of the input-pair
   2£  # Only leave the first two values
       # (after which the result is output implicitly)
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Haskell, 32 bytes

a#b=take 2[i|i<-[1..],a/=i,b/=i]

Try it online! We pick the first two positive integers that aren't input numbers.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6),  32 27  26 bytes

Takes input as (x)(y).

x=>y=>[x^=k=y^x^1?1:2,y^k]

Try it online!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 27 10 bytes

I…⁻E⁴ιE²N²

Try it online! Link is to verbose version of code. Uses the same set difference algorithm as everyone else. Less boring 27-byte version actually tries to produce interesting outputs for all inputs:

NθNηI﹪⁺÷¹²⁸⊕¬﹪⁻θη¹²⁸⟦θη⟧²⁵⁶

Try it online! Link is to verbose version of code. Takes unsigned integers. Explanation: Flips the top bits of the values, unless this would exchange them, in which case adds 64 (modulo 256) instead.

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

brainfuck, 69 bytes

,+[->+>+>+<<<],[->>->-<<<]++>>>[[-]<<<->.>>]<+<+>[[-]<<->.>]<+<[->.<]

Try it online!

My solution takes the first input and counts it upwards and prints the first two numbers unless they are the second input. So if the first input is A and the second input is B the output will be:
A+2, A+3 if B = A+1 or
A+1, A+3 if B = A+2 or
A+1, A+2 else

This initializes position 1,2,3 of the band as the first input incremented by 1
,[->>->-<<<]
This substracts the second input from positions 2 and 3.
++
Set position 0 to 2, counting the numbers still to print
The band now reads: (2) a+1 a-b+1 a-b+1
>>>[[-]<<<->.>>]
If position 3 is not 0, print out position 1 and decrement position 0
<+<+>
Increment the band in position 1 and 2
[[-]<<->.>]
If position 2 is not 0, print out position 1 and derement position 0
<+<[->.<]
Increment at position 1, Position 0 is now either 1 or 0. If it is 1 print position 1 one more time. ```
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 32 bytes

port of @Jo King's answer

Complement[Range@4,{##}][[;;2]]&

Try it online!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Python 3, 27 bytes

lambda s:[*{0,1,2,3}-s][:2]

Try it online! The input is a set s with the integers. We build the set {0, 1, 2, 3, 4, 5, 6, 7, 8} and then perform set subtraction to remove, from that set, any input numbers. We then convert to a list and extract two elements.

Thanks Surculose for saving me 2!

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ {0,1,2,3}-s should works \$\endgroup\$ – Surculose Sputum Mar 23 at 14:47
1
\$\begingroup\$

Octave, 28 22 bytes

@(x)setxor(1:4,x)(1:2)

Try it online!

Explanation

Anonymous function that outputs the first 2 entries from the sorted symmetric difference between [1 2 3 4] and the input.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Japt, 8 bytes

Hõ kU ¯2

Try it

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Python 3.8, 31 29 bytes

Saved 2 bytes thanks to xnor!!!

lambda x,y:(k:=~x&1|~y&2,k+4)

Try it online!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ The parens around the first entry aren't necessary. \$\endgroup\$ – xnor Mar 23 at 22:34
  • \$\begingroup\$ @xnor Interesting - thanks! :-) \$\endgroup\$ – Noodle9 Mar 23 at 23:12
  • \$\begingroup\$ k:=(x|1)^(y|2) seems like a it could save bytes, but I don't a way to get rid of the parens. \$\endgroup\$ – xnor Mar 25 at 1:24
1
\$\begingroup\$

Pyth, 5 bytes

>2-Uy

Try it online!

Explanation

    y # Repeat the input
   U  # Generate a length range: [0 .. 3]
  -   # Difference with implicit input
>2    # Slice out the last 2 items of the resulting list
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ You probably mean "repeat" the input, not "repeat twice"? \$\endgroup\$ – Luis Mendo Mar 23 at 18:53
  • \$\begingroup\$ y is powerset, not repeat the input. Still length 4, which is what counts. \$\endgroup\$ – isaacg Mar 24 at 4:11
1
\$\begingroup\$

C (gcc), 36 34 28 bytes

Saved 2 8 bytes thanks to ceilingcat!!!

f(x,y){x=(~y&2|~x&1)*257+4;}

Try it online!

Uses Paul Mutser's formula.
Returns the two 16-bit values in a 32-bit int since there are no tuples &c in C.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Bash + Core utilities, 29 bytes

seq 4|egrep -v $1\|$2|head -2

Try it online!

Input is passed in two arguments, and the output is printed to stdout.

This prints the first two positive integers that aren't either of the two arguments.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ This seems to give only one number if passed in 1 and 2. \$\endgroup\$ – xnor Mar 23 at 20:50
  • \$\begingroup\$ @xnor Thanks -- that was a dumb mistake. It's fixed now (no scoring change). \$\endgroup\$ – Mitchell Spector Mar 24 at 5:52
0
\$\begingroup\$

Erlang (escript), 43 bytes

Outputs the first two numbers from the range 0 to 3 that aren't in the input.

f(A)->lists:sublist(lists:seq(0,3)--A,1,2).

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

W, 5 bytes

Takes input as a pair of integers.

%G?←√

Uncompressed:

4k     Range from 1 to 4
  at   Trim out all items that appear in the input
    2< Take the first two items of the output list.
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Io, 46 bytes

Port of RGS's Python answer.

method(x,list(1,2,3,4)difference(x)slice(0,2))

Try it online!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Alchemist, 60 bytes

_->In_x+In_y+2n
n+x+y->Out_z+z+Out_" "
x+0y->z+3y
y+0x->z+3x

Try it online!

As with many answers here, prints the first two non-negative integers not in the input. Input is unsigned, since Alchemist doesn't have signed numbers.

I similarly can't just let a variable "go negative" after it hits zero, so I just let an input value x also exclude x+4, x+8, and so on. This has no effect on anything, provided both variables don't hit zero at the same time. Fortunately, if they ever become the same, both outputs will happen before they hit zero.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Ruby, 22 bytes

->*a{([*6..9]-a)[0,2]}

Try it online!

Same method as almost everybody.

|improve this answer|||||
\$\endgroup\$
-2
\$\begingroup\$

CommonJS (unsig,ugly,selfcnt) 48B

m=n=>(n+1)%256;module.exports=a=>b=>[m(a),m(b)];

Readable version:

submission = number => (n + 1) % 256; // Add 1, wrapping around.
module.exports = number1 => (number2 => [submission(number1), submission(number2)]); // Apply submission to the input numbers.

JavaScript (unsig,ugly,expr) 43B

a=>b=>[(n=>(n+1)%256)(a),(n=>(n+1)%256)(b)];

Readable version:

Nope, you're on your own on this one. \$:D\$

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ What if your inputs are 255 and 0? Then your outputs are 0 and 1, so you have 0 twice. I haven't thought of any way to avoid doing some kind of interaction between the two inputs to make sure that f(a) != b for any function f you can create. e.g. rotating across the whole 16-bit b:a doesn't work, neither does bitwise-NOT. Which is too bad because otherwise an x86 assembly answer could take both inputs in the AH:AL sub-registers of AX and just need one instruction plus a ret. \$\endgroup\$ – Peter Cordes Mar 25 at 3:36
  • \$\begingroup\$ @PeterCordes Oh yeah, stupid wraparound. I'll edit. \$\endgroup\$ – Nathan Kulzer Apr 1 at 2:58
  • \$\begingroup\$ @downvoters Why the downvotes? \$\endgroup\$ – Nathan Kulzer Apr 1 at 3:00
  • \$\begingroup\$ I downvoted because the algorithm is fundamentally flawed, like I explained in a comment. It's normal on codegolf to downvote answers that don't work. Brain farts happen, and remember it's a vote on the answer on its own merits not on you personally. (This question unfortunately doesn't have any test cases, but I guess that's because no specific result is required. It does encourage you to exhaustively test for all 2^16 possible inputs, which would have caught your problem. Also, the whole point of % is wraparound, and disallowing f(a) == b is what makes this problem interesting.) \$\endgroup\$ – Peter Cordes Apr 1 at 3:08

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.