82
\$\begingroup\$

According to this XKCD comic, there is a formula to determine whether or not the age gap in a relationship is "creepy". This formula is defined as:

(Age/2) + 7

being the minimum age of people you can date.

Therefore a relationship is creepy if either of the people in said relationship are younger than the minimum age of the other.

Given the age of two people, can you output whether or not that relationship is creepy?

Rules

  1. Your program should take two integers as input, the age of both people in the relationship. These can be taken in any reasonable format.

  2. Your program must then output a truthy or falsy value describiing whether or not the relationship is "creepy" (Truthy = Creepy).

  3. Standard loopholes are not allowed.
  4. This puzzle is Code Golf, so the answer with the shortest source code in bytes wins

Test Cases

40, 40    - Not Creepy
18, 21    - Not Creepy
80, 32    - Creepy
15, 50    - Creepy
47, 10000 - Creepy
37, 38    - Not Creepy
22, 18    - Not Creepy
\$\endgroup\$
13
  • 4
    \$\begingroup\$ How should age/2 be rounded? Probably up if the exact half is supposed to be the minimum? 17,21 would make a good test case. \$\endgroup\$ May 24, 2017 at 18:20
  • 5
    \$\begingroup\$ @MartinEnder The value is a minimum, so do not round at all. It doesn't have to be an integer. \$\endgroup\$
    – Leo
    May 24, 2017 at 18:24
  • 86
    \$\begingroup\$ You could also add 13, 13 - Creepy. \$\endgroup\$ May 24, 2017 at 19:20
  • 14
    \$\begingroup\$ 47, 10000 is an... interesting combination. I would also like to point out that according to this formula, it is creepy for Doctor Who to date any human. \$\endgroup\$ May 26, 2017 at 15:01
  • 9
    \$\begingroup\$ @DavidConrad - well yeah. its basically beastiality on his part.... \$\endgroup\$
    – Batman
    May 26, 2017 at 16:47

74 Answers 74

1 2
3
0
\$\begingroup\$

Actually, 12 bytes

;RZ⌠i½7+≤⌡Mπ

Try it online, or run all test cases!

Outputs 1 if it's not creepy, and 0 if it is.

Explanation:

;RZ⌠i½7+≤⌡Mπ  implicit input: [a, b]
;RZ           zip input with its reverse ([[a,b], [b,a]])
   ⌠i½7+≤⌡M   for each pair:
    i           flatten
     ½7+        half of one plus 7
        ≤       is less than or equal to the other?
           π  product (acts as a boolean AND)
\$\endgroup\$
0
\$\begingroup\$

tcl, 59

scan $argv %d\ %d x y
puts [expr max($x,$y)/2+7>min($x,$y)]

demo

To try, type

tclsh main.tcl Age1 Age2

on the green area.

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

Tcl, 44

I was losing to answers implementing it as a function, so I had to make mine into a function also:

proc c x\ y {expr max($x,$y)/2+7>min($x,$y)}

demo — You just have to press on the "Execute" button and see the 0 and 1 digits on the green area matching the question's creepiness values.

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

Wolfram Language, 16 bytes

Min@#<Max@#/2+7&

Takes input as a list.

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

S.I.L.O.S, 64 bytes

readIO
j=i
readIO
x=i/2+7-j
y=j/2+7-i
if x c
if y c
print k
lblc

Try it online!

It prints out the letter "k" if the relationship is ok, otherwise it prints out nothing. I couldn't figure out any mathemagical way with standard mathematical operators to do an if x>0 pr y>0 to save bytes.


Alternatively, we can create an "or gate" by exploiting the fact that x^.5 will error out if the number is negative.This removes the need for a conditional, but lengthens our code. This version outputs 0 for creepy and some positive number for not creepy.

S.I.L.O.S, 67 bytes

readIO
j=i
readIO
x=0-i/2-7+j
y=0-j/2-7+i
a=x^.5+y^.5+1
printInt a

Try it online!

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

Alice, 21 18 bytes

/o
\i@/sh2:-6-.H+n

Try it online!

-3 bytes thanks to Martin Ender.

Input is in any format that includes exactly two integers. Output is 1 if creepy and 0 if not creepy.

Explanation

ish2:-6-.H+no

i                Input numbers
 s               Sort the two ages
  h2:            Divide larger age by 2 rounded up
     -           Subtract from smaller age
      6-         Subtract 6
        .H+      Determine whether result is negative
           n     Push 1 if was negative, 0 otherwise
            o    Output
\$\endgroup\$
1
  • \$\begingroup\$ Would it be possible to shorten the formula by sorting the inputs with s (which puts the max on top and the min below)? \$\endgroup\$ May 30, 2017 at 9:15
0
\$\begingroup\$

MATL, 10 bytes

tEP-X>-14>

My first ever solution. :) Try it online!

Explainer:

t    % push the input array [a b] (possibly redundant)
EP   % push the flipped(P)+doubled(E) array [2b 2a]
-    % subtract to get [a-2b b-2a]
X>   % take the maximum
-14> % if either exceeds -14, then it is creepy af bro!
\$\endgroup\$
0
\$\begingroup\$

Ly, 8 bytes

a2/7+Lfp

Try it online!

Explanation:

a2/7+Lfp

a        # sort (implicit input)
 2/7+    # halve and add 7
     L   # less than?
      fp # pop value, leaving just the result
         # implicit output
\$\endgroup\$
0
\$\begingroup\$

Dyalog APL, 11 bytes

⌊<(⌊7+.5×⌈)

Try it online!

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

C, C++, Java, C#, D, 56 bytes

-1 byte thanks to Zacharý

int c(int a,int b){return((a>b?a:b)/2+7)>(a>b?b:a)?1:0;}

C, C++, D optimization : 52 bytes

int c(int a,int b){return((a>b?a:b)/2+7)>(a>b?b:a);}
  • C++ convert bool object to int implicitly ( false = 0, true = 1 )
  • In C, booleans are represented by integers values, where 0 is false, and 1 is true.
  • I barely know how to print something in D, so i won't comment about boolean to int conversions in this language Zacharý just informed me that D bool to int conversion works exactly like in C/C++
  • Unfortunately, Java and C# can't convert an integer to a boolean, so we need to use ternary operator.
\$\endgroup\$
0
0
\$\begingroup\$

Sakura, 7 bytes

>Ƣ+ħ⓪7ƣ

Takes input as an array and outputs -1 if creepy and 0 otherwise.

Explanation:

>                     >
 Ƣ      max(         )
  +                +
   ħ             /2
    ⓪       input
     7              7
      ƣ                min(     )
                           input
\$\endgroup\$
0
\$\begingroup\$

q/kdb+, 17 16 bytes

Solution:

{(x&y)<7+.5*x|y}

Try it online! (oK rather than q/kdb+)

Examples:

q){(x&y)<7+.5*x|y}[22;18]
0b
q){(x&y)<7+.5*x|y}[22;17]
1b

Explanation:

Outputs creepy as boolean 1b (true, creepy) or 0b (false, not creepy)

{(x&y)<7+.5*x|y} / solution
{              } / lambda function with implicit parameters x and y
            x|y  / max of x and y
         .5*     / multiply by 0.5 (aka divide by 2)
       7+        / add 7
      <          / less than?
 (x&y)           / min of x and y

Notes:

Polyglot in:

  • q/kdb+
  • K
  • oK (see TIO link)
\$\endgroup\$
0
\$\begingroup\$

x86-64 machine code, 16 bytes

Takes the ages in edi, esi, returns 0 or 1 in eax, following x86-64 SysV ABI for bool creepy(int, int);.

39 f7 7d 02 87 fe 8d 44  36 f2 29 f8 c1 e8 1f c3

Assembly source follows. We test 2*young - 14 - old < 0.

creepy: 
        cmp     edi, esi        
        jge     ready
        xchg    edi, esi
ready:
        ;;  higher age now in edi
        lea     eax, [rsi*2-14]
        ;; eax = highest age the younger person can date
        sub     eax, edi
        ;; if result is negative, then creepy
        shr     eax, 31
        ret
\$\endgroup\$
0
\$\begingroup\$

ARM64 machine code, 24 bytes

Takes the ages in w0, w1, returns 0 or 1 in w0, following normal ARM64 calling conventions for bool creepy(int, int);. (You could handle 64-bit ages by replacing all w with x registers, with no increase in size.)

4b010402 
4b000423 
3100385f 
3a4ed860 
1a9fd7e0 
d65f03c0 

Assembly source follows. We test x-2*y > -14 || y-2*x > -14.

creepy:
    sub     w2, w0, w1, lsl #1
    sub     w3, w1, w0, lsl #1
    cmn     w2, #14
    ccmn    w3, #14, #0, le     // #0 = none of NZCV, makes gt true
    cset    w0, gt
    ret
\$\endgroup\$
1 2
3

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.