Equal, sum or difference!

32

2

Write shortest possible code that will return true if the two given integer values are equal or their sum or absolute difference is 5.

Example test cases:

4 1 => True
10 10 => True
1 3 => False
6 2 => False
1 6 => True
-256 -251 => True
6 1 => True
-5 5 => False

The shortest I could come up with in python2 is 56 characters long:

x=input();y=input();print all([x-y,x+y-5,abs(x-y)-5])<1

-9, thanks @ElPedro. It takes input in format x,y:

x,y=input();print all([x-y,x+y-5,abs(x-y)-5])<1

Vikrant Biswas

Posted 2019-01-16T15:58:22.997

Reputation: 429

9

welcome to PPCG! This is a good first challenge -- the challenge is clearly defined, it has ample test cases, and uses our default I/O! If you stick around for a while and keep thinking up interesting challenges, I would recommend using The Sandbox to get feedback before posting them to this site. I hope you enjoy the time you spend here!

– Giuseppe – 2019-01-16T16:22:03.320

Answers

22

Python 2, 30 bytes

lambda a,b:a in(b,5-b,b-5,b+5)

Try it online!

One byte saved by Arnauld

Three bytes saved by alephalpha

ArBo

Posted 2019-01-16T15:58:22.997

Reputation: 1 416

This is amazingly concise, thanks – Vikrant Biswas – 2019-01-16T17:17:56.290

Same can be done in Octave/MATLAB in 29 bytes (Try it online!).

– Tom Carpenter – 2019-01-17T18:51:02.000

17

JavaScript (ES6), 28 bytes

Takes input as (a)(b). Returns \$0\$ or \$1\$.

a=>b=>a+b==5|!(a-=b)|a*a==25

Try it online!

Arnauld

Posted 2019-01-16T15:58:22.997

Reputation: 111 334

1Damn, took me a long long time to figure out how this handling the difference part. :) – Vikrant Biswas – 2019-01-16T19:34:24.123

9

Dyalog APL, 9 bytes

=∨5∊+,∘|-

Try it online!

Spelled out:

  =   ∨  5      ∊                +   , ∘    |            -
equal or 5 found in an array of sum and absolute of difference.

dzaima

Posted 2019-01-16T15:58:22.997

Reputation: 19 048

8

x86 machine code, 39 bytes

00000000: 6a01 5e6a 055f 5251 31c0 39d1 0f44 c601  j.^j._RQ1.9..D..
00000010: d139 cf0f 44c6 595a 29d1 83f9 050f 44c6  .9..D.YZ).....D.
00000020: 83f9 fb0f 44c6 c3                        ....D..

Assembly

section .text
	global func
func:					;inputs int32_t ecx and edx
	push 0x1
	pop esi
	push 0x5
	pop edi
	push edx
	push ecx
	xor eax, eax

	;ecx==edx?
	cmp ecx, edx
	cmove eax, esi

	;ecx+edx==5?
	add ecx, edx
	cmp edi, ecx
	cmove eax, esi
	
	;ecx-edx==5?
	pop ecx
	pop edx
	sub ecx, edx
	cmp ecx, 5
	
	;ecx-edx==-5?
	cmove eax, esi
	cmp ecx, -5
	cmove eax, esi

	ret

Try it online!

Logern

Posted 2019-01-16T15:58:22.997

Reputation: 845

5

J, 12 11 bytes

1 byte saved thanks to Adám

1#.=+5=|@-,+

Try it online!

Explanation

This is equivalent to:

1 #. = + 5 = |@- , +

This can be divided into the following fork chain:

(= + (5 e. (|@- , +)))

Or, visualized using 5!:4<'f':

  ┌─ =               
  ├─ +               
──┤   ┌─ 5           
  │   ├─ e.          
  └───┤          ┌─ |
      │    ┌─ @ ─┴─ -
      └────┼─ ,      
           └─ +      

Annotated:

  ┌─ =                                     equality
  ├─ +                                     added to (boolean or)
──┤   ┌─ 5                                   noun 5
  │   ├─ e.                                  is an element of
  └───┤          ┌─ |  absolute value         |
      │    ┌─ @ ─┴─ -  (of) subtraction       |
      └────┼─ ,        paired with            |
           └─ +        addition               | any of these?

Conor O'Brien

Posted 2019-01-16T15:58:22.997

Reputation: 36 228

Save a byte with e. – Adám – 2019-01-16T16:58:32.473

@Adám How so? Shortest approach I got with e. was =+.5 e.|@-,+. Maybe you forget 5e. is an invalid token in J? – Conor O'Brien – 2019-01-16T17:05:18.283

1Since two integers cannot simultaneously sum to 5 and be equal, you can use + instead of +. – Adám – 2019-01-16T17:11:02.650

@Adám Ah, I see, thank you. – Conor O'Brien – 2019-01-16T17:15:43.177

5

Python 2, 29 31 bytes

lambda a,b:a+b==5or`a-b`in"0-5"

Try it online!

Since I didn't manage to read the task carefully the first time, in order to fix it, I had to come up with a completely different approach, which is unfortunately not as concise.

Kirill L.

Posted 2019-01-16T15:58:22.997

Reputation: 6 693

5

R, 40 bytes (or 34)

function(x,y)any((-1:1*5)%in%c(x+y,x-y))

Try it online!

For non-R users:

  • -1:1*5 expands to [-5, 0, 5]
  • the %in% operator takes elements from the left and checks (element-wise) if they exist in the vector on the right

A direct port of @ArBo's solution has 35 34 bytes, so go upvote that answer if you like it:

function(x,y)x%in%c(y--1:1*5,5-y)

ngm

Posted 2019-01-16T15:58:22.997

Reputation: 3 974

The 34 byte one can be reduced by 1 with function(x,y)x%in%c(y--1:1*5,5-y) – MickyT – 2019-01-16T21:36:57.037

Can drop to 30 bytes by moving the subtraction: function(x,y)(x-y)%in%(-1:1*5), and drop it further to 24 bytes by dropping the function notation to scan() input: diff(scan())%in%(-1:1*5) Try it online!.

Still very much the same method though.

– CriminallyVulgar – 2019-01-17T14:38:46.187

1@CriminallyVulgar does that account for the sum being 5? – ArBo – 2019-01-17T15:45:43.857

@ArBo Hah, missed that in the spec, and there wasn't a test case in the TIO so I just glossed over it! – CriminallyVulgar – 2019-01-18T09:39:02.813

Minor change that can be made to both is to use pryr::f, which happens to work in both cases. Whether it can properly detect the arguments is entirely somewhat hit or miss but it seems to nail these two functions. e.g. pryr::f(x%in%c(y--1:1*5,5-y)) Try it online!.

Gets you to 36 and 29 bytes respectively.

– CriminallyVulgar – 2019-01-18T15:50:44.420

5

8086 machine code, 22 20 bytes

8bd0 2bc3 740e 7902 f7d8 3d0500 7405 03d3 83fa05

Ungolfed:

ESD  MACRO
    LOCAL SUB_POS, DONE
    MOV  DX, AX     ; Save AX to DX
    SUB  AX, BX     ; AX = AX - BX
    JZ   DONE       ; if 0, then they are equal, ZF=1
    JNS  SUB_POS    ; if positive, go to SUB_POS
    NEG  AX         ; otherwise negate the result
SUB_POS:
    CMP  AX, 5      ; if result is 5, ZF=1
    JZ   DONE
    ADD  DX, BX     ; DX = DX + BX
    CMP  DX, 5      ; if 5, ZF=1
DONE:
    ENDM

Input numbers in AX and BX and returns Zero Flag (ZF=1) if result is true. If desired, you can also determine which condition was true with the following:

  • ZF = 1 and DX = 5 ; sum is 5
  • ZF = 1 and AX = 5 ; diff is 5
  • ZF = 1 and AX = 0 ; equal
  • ZF = 0 ; result false

If the difference between the numbers is 0, we know they are equal. Otherwise if result is negative, then first negate it and then check for 5. If still not true, then add and check for 5.

Sample PC DOS test program. Download it here (ESD.COM).

START:
    CALL INDEC      ; input first number into AX
    MOV  BX, AX     ; move to BX
    CALL INDEC      ; input second number into BX
    ESD             ; run "Equal, sum or difference" routine
    JZ   TRUE       ; if ZF=1, result is true
FALSE:
    MOV  DX, OFFSET FALSY   ; load Falsy string
    JMP  DONE
TRUE:
    MOV  DX, OFFSET TRUTHY  ; load Truthy string
DONE:
    MOV  AH, 9      ; DOS display string
    INT  21H        ; execute
    MOV  AX, 4C00H  ; DOS terminate
    INT  21H        ; execute

TRUTHY   DB 'Truthy$'
FALSY    DB 'Falsy$'

INCLUDE INDEC.ASM   ; generic decimal input prompt routine

Output of test program:

A>ESD.COM
: 4
: 1
Truthy

A>ESD.COM
: 10
: 10
Truthy

A>ESD.COM
: 1
: 3
Falsy

A>ESD.COM
: 6
: 2
Falsy

A>ESD.COM
: 1
: 6
Truthy

A>ESD.COM
: -256
: -251
Truthy

A>ESD.COM
: 6
: 1
Truthy

A>ESD.COM
: 9999999999
: 9999999994
Truthy

640KB

Posted 2019-01-16T15:58:22.997

Reputation: 7 149

4

Python 2, 38 bytes

-2 bytes thanks to @DjMcMayhem

lambda a,b:a+b==5or abs(a-b)==5or a==b

Try it online!

fəˈnɛtɪk

Posted 2019-01-16T15:58:22.997

Reputation: 4 166

Your TIO is actually 42 bytes but you can fix it by deleting the spaces between the 5s and the ors – ElPedro – 2019-01-16T16:12:04.340

3

Actually, the TIO link could be 38 bytes

– James – 2019-01-16T16:14:42.730

@ElPedro the function itself was 40 bytes but I used f= in order to be able to call it – fəˈnɛtɪk – 2019-01-16T18:10:27.593

1@DJMcMayhem I don't normally golf in python. I just did it because the question asker used python for their example – fəˈnɛtɪk – 2019-01-16T18:12:10.927

4

Jelly, 7 bytes

+,ạ5eo=

Try it online!

How it works

+,ạ5eo=  Main link. Arguments: x, y (integers)

+        Yield x+y.
  ạ      Yield |x-y|.
 ,       Pair; yield (x+y, |x-y|).
   5e    Test fi 5 exists in the pair.
      =  Test x and y for equality.
     o   Logical OR.

Dennis

Posted 2019-01-16T15:58:22.997

Reputation: 196 637

4

PowerShell, 48 44 40 bytes

param($a,$b)$b-in($a-5),(5-$a),(5+$a),$a

Try it online! or Verify all Test Cases

Takes input $a and $b. Checks if $b is -in the group ($a-5, 5-$a 5+$a, or $a), which checks all possible combinations of $a,$b, and 5.

-4 bytes thanks to mazzy.
-4 bytes thanks to KGlasier.

AdmBorkBork

Posted 2019-01-16T15:58:22.997

Reputation: 41 581

($a-$b) is -$x :) – mazzy – 2019-01-16T17:14:08.183

@mazzy Ooo, good call. – AdmBorkBork – 2019-01-16T17:41:00.380

If you switch 5 and $b around you can cut off a couple bytes(ie param($a,$b)$b-in($a-5),(5-$a),($a+5),$a) Try it out here

– KGlasier – 2019-01-17T15:13:21.343

1@KGlasier Excellent suggestion. I needed to swap $a+5 to 5+$a to get it to cast appropriately when taking command-line input, but otherwise awesome. Thanks! – AdmBorkBork – 2019-01-17T15:55:10.873

4

Wolfram Language (Mathematica), 22 bytes

Takes input as [a][b].

MatchQ[#|5-#|#-5|#+5]&

Try it online!

alephalpha

Posted 2019-01-16T15:58:22.997

Reputation: 23 988

4

Java (JDK), 30 bytes

a->b->a+b==5|a==b|(b-=a)*b==25

Try it online!

Olivier Grégoire

Posted 2019-01-16T15:58:22.997

Reputation: 10 647

4

Pascal (FPC), 26 70 bytes

Edit: + input variables.

Procedure z(a,b:integer);begin Writeln((abs(a-b)in[0,5])or(a+b=5))end;

Try it online!


(abs(a-b)in[0,5])or(a+b=5)

Try it online!

I hope that my answer is according to all rules of code-golf. It was fun anyway.

Dessy Stoeva

Posted 2019-01-16T15:58:22.997

Reputation: 41

2Hello, and welcome to PPCG! Normally, you have to take input, instead of assuming it is already in variables. I don't know Pascal, but I think that is what this code is doing. – NoOneIsHere – 2019-01-20T17:42:27.887

Hello, NoOneIsHere and thank you for the remark. It was may concern too - shall I include the initialization of the variables. Looking at several other solutions, like Java for example, where the function definition with parameters was excluded from the total length of the solution, I decided not to include ReadLn. – Dessy Stoeva – 2019-01-20T18:42:38.870

Alright. Welcome to PPCG! – NoOneIsHere – 2019-01-20T23:47:42.963

The Java submission is an anonymous lambda which takes two parameters. This appears to use predefined variables, which is not a valid method of input. – Jo King – 2019-01-24T11:08:36.903

1No problem, I will change my submission. – Dessy Stoeva – 2019-01-24T13:55:31.760

3

C# (.NET Core), 43, 48, 47, 33 bytes

EDIT: Tried to use % and apparently forgot how to %. Thanks to Arnauld for pointing that out!

EDIT2: AdmBorkBork with a -1 byte golf rearranging the parentheses to sit next to the return so no additional space is needed!

EDIT3: Thanks to dana for -14 byte golf for the one-line return shortcut and currying the function (Ty Embodiment of Ignorance for linking to TIO).

C# (.NET Core), 33 bytes

a=>b=>a==b|a+b==5|(a-b)*(a-b)==25

Try it online!

Destroigo

Posted 2019-01-16T15:58:22.997

Reputation: 401

Bah. Trying to avoid System.Math. Back to it! Thanks for pointing that out :D – Destroigo – 2019-01-16T16:41:32.023

1

You can get it down to 33 bytes applying dana's tips

– Embodiment of Ignorance – 2019-01-16T18:10:06.353

3

Scala, 43 bytes

def f(a:Int,b:Int)=a+b==5|(a-b).abs==5|a==b

Try it online!

Xavier Guihot

Posted 2019-01-16T15:58:22.997

Reputation: 223

Isn't it possible to golf the || to |? I know it's possible in Java, C#, Python, or JavaScript, but not sure about Scala. – Kevin Cruijssen – 2019-01-17T12:15:02.843

Actually yes! thanks – Xavier Guihot – 2019-01-17T12:18:02.670

3

Perl 6, 24 bytes

-1 byte thanks to Grimy

{$^a-$^b==5|0|-5|5-2*$b}

Try it online!

This uses the Any Junction but technically, ^ could work as well.

Explanation:

{                      }  # Anonymous code block
 $^a-$^b==                # Is the difference equal to
           | |  |        # Any of
          0 
            5
              -5
                 5-2*$b

Jo King

Posted 2019-01-16T15:58:22.997

Reputation: 38 234

1-1 byte with {$^a-$^b==5|0|-5|5-2*$b} – Grimmy – 2019-01-17T15:00:54.307

3

C (gcc), 33 bytes

f(a,b){a=!(a+b-5&&(a-=b)/6|a%5);}

Try it online!

Tried an approach I didn't see anyone else try using. The return expression is equivalent to a+b==5||((-6<a-b||a-b<6)&&(a-b)%5==0).


attinat

Posted 2019-01-16T15:58:22.997

Reputation: 3 495

2

C (gcc), 41 34 bytes

f(a,b){a=5==abs(a-b)|a+b==5|a==b;}

Try it online!

cleblanc

Posted 2019-01-16T15:58:22.997

Reputation: 3 360

1Why does f return a? Just some Undefined Behavior? – Tyilo – 2019-01-16T17:05:49.870

@Tyilo Yes, it's implementation specific. So happens the first parameter is stored in the same register as the return value. – cleblanc – 2019-01-16T17:07:16.690

30 bytes Try it online! – Logern – 2019-01-16T18:02:51.240

@Logern Doesn't work for f(6,1) – cleblanc – 2019-01-16T18:31:54.057

@ceilingcat Doesn't work for f(6,1) – cleblanc – 2019-01-16T18:32:07.083

33 bytes – ceilingcat – 2019-01-17T00:01:57.587

@ceilingcat The logic is reversed. It returns false when it should return true. – cleblanc – 2019-01-17T14:12:08.730

2

05AB1E, 13 12 bytes

ÐO5Qs`α5QrËO

Try it online!

Takes input as a list of integers, saving one byte. Thanks @Wisław!

Alternate 12 byte answer

Q¹²α5Q¹²+5QO

Try it online!

This one takes input on separate lines.

Cowabunghole

Posted 2019-01-16T15:58:22.997

Reputation: 1 590

1Since it is not very clearly specified, can you not assume the input is a list of integers, thus eliminating the initial |? – Wisław – 2019-01-16T17:22:20.813

@Wisław Good point, I updated my answer. Thanks! – Cowabunghole – 2019-01-16T17:28:08.277

I found a 11 bytes alternative: OI\αª5¢IË~Ā`. Input is a list of integers. – Wisław – 2019-01-16T17:54:29.250

1OIÆÄ)5QIËM is 10. – Magic Octopus Urn – 2019-01-16T18:35:43.917

1@MagicOctopusUrn I'm not sure exactly what the rules are but I think your solution is different enough from mine to submit your own answer, no? Also, unrelated but I've seen your username on this site for a long time but only after typing it out did I realize that it's "Urn", not "Um" :) – Cowabunghole – 2019-01-16T18:43:09.623

I already posted a different solution, but that's the one I got to while playing with yours so feel free to use it. Also I've always been annoyed at that rn=m with the SE font lol! Magic Octopus Urn is an anagram for my last_name+computing. Dennis chose it for me out of a list of anagrams. – Magic Octopus Urn – 2019-01-16T18:58:33.417

Should've gone with Mr Contagious Cup, Magic Coupon Rust, or Narcotic Gum Soup – Magic Octopus Urn – 2019-01-16T19:03:31.477

2

05AB1E, 10 bytes

OIÆ‚Ä50SåZ

Try it online!


O           # Sum the input.
 IÆ         # Reduced subtraction of the input.
   ‚        # Wrap [sum,reduced_subtraction]
    Ä       # abs[sum,red_sub]
     50S    # [5,0]
        å   # [5,0] in abs[sum,red_sub]?
         Z  # Max of result, 0 is false, 1 is true.

Tried to do it using stack-only operations, but it was longer.

Magic Octopus Urn

Posted 2019-01-16T15:58:22.997

Reputation: 19 422

1This will unfortunately return true if the sum is 0 such as for [5, -5] – Emigna – 2019-01-16T20:28:29.177

1Your other 10-byte solution that you left as a comment (OIÆÄ‚5QIËM) is correct for [5,-5]. – Kevin Cruijssen – 2019-01-17T09:31:40.200

Another 10-byte solution that I came up with is OsÆÄ‚5åsË~. Almost identical to yours it seems. Try it online!

– Wisław – 2019-01-17T10:54:38.550

2

Ruby, 34 Bytes

->(a,b){[a+5,a-5,5-a,a].include?b}

Online Eval - Thanks @ASCII-Only

Jatin Dhankhar

Posted 2019-01-16T15:58:22.997

Reputation: 141

do you check if they're equal though... – ASCII-only – 2019-01-20T12:20:08.867

Oops, forgot to add that check. Thanks @ASCII-only for pointing out the mistake. – Jatin Dhankhar – 2019-01-20T12:23:11.703

1

i'd be nice if you could link to this

– ASCII-only – 2019-01-21T02:57:17.407

this might be valid? not completely sure though, you might wanna check with someone else – ASCII-only – 2019-01-28T04:45:09.440

This will work but it requires .nil? check to give output in the required format. ->(a,b){[a+5,a-5,5-a,a].index(b).nil?}, this is longer than the current one. – Jatin Dhankhar – 2019-01-28T05:04:15.837

Oh yeah, the question says "return true" – ASCII-only – 2019-01-28T09:22:29.173

1

Japt, 14 13 bytes

¥VªaU ¥5ª5¥Nx

Try it online!

Oliver

Posted 2019-01-16T15:58:22.997

Reputation: 7 160

1

Tcl, 53 bytes

proc P a\ b {expr abs($a-$b)==5|$a==$b|abs($a+$b)==5}

Try it online!

sergiol

Posted 2019-01-16T15:58:22.997

Reputation: 3 055

1

Japt, 13 12 bytes

x ¥5|50ìøUra

Try it or run all test cases

x ¥5|50ìøUra
                 :Implicit input of array U
x                :Reduce by addition
  ¥5             :Equal to 5?
    |            :Bitwise OR
     50ì         :Split 50 to an array of digits
        ø        :Contains?
         Ur      :  Reduce U
           a     :    By absolute difference

Shaggy

Posted 2019-01-16T15:58:22.997

Reputation: 24 623

Fails for [-5,5] (should be falsey) – Kevin Cruijssen – 2019-01-17T09:26:07.627

Thanks, @KevinCruijssen. Rolled back to the previous version. – Shaggy – 2019-01-17T09:50:01.617

1

Batch, 81 bytes

@set/as=%1+%2,d=%1-%2
@if %d% neq 0 if %d:-=% neq 5 if %s% neq 5 exit/b
@echo 1

Takes input as command-line arguments and outputs 1 on success, nothing on failure. Batch can't easily do disjunctions so I use De Morgan's laws to turn it into a conjunction.

Neil

Posted 2019-01-16T15:58:22.997

Reputation: 95 035

1

Charcoal, 18 bytes

Nθ¿№⟦θ⁺⁵θ⁻⁵θ⁻θ⁵⟧N1

Try it online! Link is to verbose version of code. Port of @ArBo's Python 2 solution.

Neil

Posted 2019-01-16T15:58:22.997

Reputation: 95 035

1

Common Lisp, 48 bytes

(lambda(a b)(find 5(list(abs(- b a))a(+ a b)b)))

coredump

Posted 2019-01-16T15:58:22.997

Reputation: 6 292

1

Brachylog, 8 bytes

=|+5|-ȧ5

Takes input as a list of two numbers (use _ for negatives). Try it online!

Explanation

Pretty much a direct translation of the spec:

=          The two numbers are equal
 |         or
  +        The sum of the two numbers
   5       is 5
    |      or
     -     The difference of the two numbers
      ȧ    absolute value
       5   is 5

DLosc

Posted 2019-01-16T15:58:22.997

Reputation: 21 213

0

Runic Enchantments, 27 bytes

i::i::}3s=?!@-'|A5:}=?!@+=@

Try it online!

Outputs 0 (exactly one zero) for false and any other output (literally whatever is left on the stack) for true.

New interpreter build, updated answer, saved 3 bytes (previous revision miscounted and said 4).

Explanation:

i::i::                         Read two inputs, duplicating each twice.
      }                        Rotate stack right
       3s                      Rotate the top 3 items to the right
         =? @                  Compare equal, if so, dump stack
           !                   If not, skip next instruction
             -'|A              Subtract, absolute value, (j)
                 5:}           Push two 5s, rotate one to the bottom
                    =? @       Compare 5 with (j), if equal, dump stack
                      ! +=     If not, add the two inputs, compare with remaining 5
                          @    Dump stack (will be 1 for true and 0 for false)

Essentially it reads both inputs 3 times and arranges the stack so that each operation works on a copy of each input (or a literal 5). As the only way to dump a 0 is if all three comparisons are false, simply dumping the rest of the stack avoids having to do anything other than vomiting the entire stack to output and terminating, all posibilities of which are valid truthy values.

Draco18s no longer trusts SE

Posted 2019-01-16T15:58:22.997

Reputation: 3 053

0

Perl 5, 51 bytes

Pretty simple really, uses the an flags for input. Outputs 0 for false, 1 for true. Not gonna lie, I don't know if bitwise OR is appropriate here, but is does work for all the test cases, so that's nice.

($a,$b)=@F;print($a==$b|$a+$b==5|$a-$b==5|$b-$a==5)

Try it online!

Geoffrey H.

Posted 2019-01-16T15:58:22.997

Reputation: 41

2bitwise OR is equivalent to logical OR when operating on one-bit values. In general, if passed a truthy arguments bitwise OR will return truthy, and if passed two falsy arguments bitwise OR will return 0. – attinat – 2019-01-18T05:24:03.447

-11 bytes – Nahuel Fouilleul – 2019-01-24T11:29:28.897

138 bytes – Nahuel Fouilleul – 2019-01-24T11:36:32.220

0

Retina 0.8.2, 82 bytes

\d+
$*
^(-?1*) \1$|^(-?1*)1{5} -?\2$|^-?(-?1*) (\3)1{5}$|^-?(1 ?){5}$|^(1 ?-?){5}$

Try it online! Link includes test cases. Explanation: The first two lines convert the inputs into unary. The final line then checks for any of the permitted matches:

^(-?1*) \1$                              x==y
^(-?1*)1{5} -?\2$   x>=0 y>=0 x=5+y i.e. x-y=5
                    x>=0 y<=0 x=5-y i.e. x+y=5
                    x<=0 y<=0 x=y-5 i.e. y-x=5
^-?(-?1*) (\3)1{5}$ x<=0 y<=0 y=x-5 i.e. x-y=5
                    x<=0 y>=0 y=5-x i.e. x+y=5
                    x>=0 y>=0 y=5+x i.e. y-x=5
^-?(1 ?){5}$        x>=0 y>=0 y=5-x i.e. x+y=5
                    x<=0 y>=0 y=5+x i.e. y-x=5
^(1 ?-?){5}$        x>=0 y>=0 x=5-y i.e. x+y=5
                    x>=0 y<=0 x=5+y i.e. x-y=5

Pivoted by the last column we get:

x==y            ^(-?1*) \1$
x+y=5 x>=0 y>=0 ^-?(1 ?){5}$
      x>=0 y>=0 ^(1 ?-?){5}$
      x>=0 y<=0 ^(-?1*)1{5} -?\2$
      x<=0 y>=0 ^-?(-?1*) (\3)1{5}$
      x<=0 y<=0 (impossible)       
x-y=5 x>=0 y>=0 ^(-?1*)1{5} -?\2$
      x>=0 y<=0 ^(1 ?-?){5}$
      x<=0 y>=0 (impossible)
      x<=0 y<=0 ^-?(-?1*) (\3)1{5}$
y-x=5 x>=0 y>=0 ^-?(-?1*) (\3)1{5}$
      x>=0 y<=0 (impossible)
      x<=0 y>=0 ^-?(1 ?){5}$
      x<=0 y<=0 ^(-?1*)1{5} -?\2$

Neil

Posted 2019-01-16T15:58:22.997

Reputation: 95 035

0

Julia 1.0, 38 bytes

f(a,b)=((a+b-5)*(a-b)*(abs(a-b)-5))==0

Try it online!

Of course, it's probably not the shortest one

trolley813

Posted 2019-01-16T15:58:22.997

Reputation: 225

0

APL(NARS) 19 chars, 38 bytes

{∨/(⍺=⍵),5∊∣⍺+⍵,-⍵}

  f←{∨/(⍺=⍵),5∊∣⍺+⍵,-⍵}
  4 f 1
1
  1 f 3
0
  6 f 2
0
  1 f 6
1
  ¯256 f ¯251
1
  6 f 1
1
  ¯5 f 5
0

RosLuP

Posted 2019-01-16T15:58:22.997

Reputation: 3 036

0

Ruby (>=1.9), 23 38 bytes

->(a,b){a==b or a+b==5 or(a-b).abs==5}

Try it online!

It's an anonymous function, so the TIO has some extra code to take input and return the output. Put the two numbers on separate lines.

Returns true if they are equal or add to 5, or if their absolute difference is 5, and false if not.

CG One Handed

Posted 2019-01-16T15:58:22.997

Reputation: 133

:| it's longer than the other (older) one – ASCII-only – 2019-01-28T04:34:29.537

also. better link

– ASCII-only – 2019-01-28T04:35:51.263