40
\$\begingroup\$

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
\$\endgroup\$
1
  • 13
    \$\begingroup\$ 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! \$\endgroup\$
    – Giuseppe
    Commented Jan 16, 2019 at 16:22

44 Answers 44

1
2
1
\$\begingroup\$

Common Lisp, 48 bytes

(lambda(a b)(find 5(list(abs(- b a))a(+ a b)b)))
\$\endgroup\$
1
\$\begingroup\$

Desmos, 90 bytes

A pretty straightfoward solution, could definitely by golfed further:

f(a,b)=\left\{a+b=5:0,1\right\}\left\{\left|a-b\right|=5:0,1\right\}\left\{a=b:0,1\right\}

Try It On Desmos!

0 is true, and 1 is false.

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

Pyth, 12 bytes

}5[sQaFQ+4l{

Try it online!

}5[sQaFQ+4l{
}5[sQaFQ+4l{Q) // Implicit input and closed brace
  [          ) // Create an array of the following:
   sQ          //   Sum of input
     aFQ       //   Absolute difference of input
        +4l{Q  //   Length of unique values in input, + 4
}5             // Is '5' in the array? 
\$\endgroup\$
1
+50
\$\begingroup\$

Vyxal, 11 10 bytes

=£₌+ĸW5c¥∨

Try it Online!

Credits

  • Saved 1 byte thanks to @Lyxal by employing the use of an absolute difference built-in
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 10 bytes ;p \$\endgroup\$
    – lyxal
    Commented Mar 2, 2021 at 1:41
1
\$\begingroup\$

Pyth, 15 bytes

|!!/,a.*QsQ5q.*

Try it online!

Not super impressed, especially having to use double negation !! to cast a number into true... I'm sure there's a better way.

|!!/,a.*QsQ5q.*
   /,      5     Count '5's in: 
     a.*Q        Absolute value of (A, B)
         sQ      Sum of (A, B)
 !!              Cast to truthy
|                OR:
            q.*  A==B
\$\endgroup\$
1
1
\$\begingroup\$

Factor, 37 bytes

[ 2dup + 5 = -rot - abs "\0"in? or ]

Try it online!

Explanation

"\0" Has literal 0 and 5 embedded in the string (you can see it on TIO). So "\0"in? is equivalent to { 0 5 } member? In other words, checking whether the input is 0 or 5.

          ! -256 -251
2dup      ! -256 -251 -256 -251
+         ! -256 -251 -507
5         ! -256 -251 -507 5
=         ! -256 -251 f
-rot      ! f -256 -251
-         ! f -5
abs       ! f 5
"\0"in?   ! f t
or        ! t
\$\endgroup\$
1
\$\begingroup\$

*><>, 33 bytes

:{:}=?;:{:}+5=?;-:0(?!u01-*O5=?;.

Try it online!

Could be lower if handling negative numbers wasn't a requirement. Uses the exit code as its truthy/falsey.

Initializes the stack with the -i flag, but this method can't handle negatives. The above code still works, but you must prepend your starting values to the start of the script. For example, the following works with the stack of [-15, 12]

53*0-c:{:}=?;:{:}+5=?;-:0(?!u01-*O5=?;.

Explanation

:{:}                                Duplicate the two values on the stack
    =?;                             If they're equal, halt execution
       :{:}                         Duplicate the two values on the stack, again
           +                        Add them together
            5=                      Check if that value is equal to 5
              ?;                    If so, halt execution
                -:                  Else, subtract our two values, and duplicate the result
                  0(                Check if the difference is less than 0
                    ?!u             If it is greater than 0, Dive and ignore instructions until we reach a Rise
                       01-*         If it isn't, multiply it by -1
                            O       Rise from our previous Dive. Ignored if there was no Dive.
                             5=     Check if our difference is equal to 5
                               ?;   If so, halt execution
                                 .  Else, throw an error
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ bitwise 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. \$\endgroup\$
    – att
    Commented Jan 18, 2019 at 5:24
  • \$\begingroup\$ -11 bytes \$\endgroup\$ Commented Jan 24, 2019 at 11:29
  • 1
    \$\begingroup\$ 38 bytes \$\endgroup\$ Commented Jan 24, 2019 at 11:36
0
\$\begingroup\$

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

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

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
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ :| it's longer than the other (older) one \$\endgroup\$
    – ASCII-only
    Commented Jan 28, 2019 at 4:34
  • \$\begingroup\$ also. better link \$\endgroup\$
    – ASCII-only
    Commented Jan 28, 2019 at 4:35
0
\$\begingroup\$

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.

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

Haskell, 30 Bytes

a?b=elem 5[a-b+5,abs(a-b),a+b]
\$\endgroup\$
0
\$\begingroup\$

Vyxal, 8 bytes

₍+ε5c□≈∨

Try it Online!

\$\endgroup\$
1
2

Your Answer

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

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