# Number in overlapping range

You are given two integers between lets say 1 and 12, one and two
and a integer between lets say 0 and 4 which is called range.
The challange is to decide if one is inside or equal the range from two ± range.

The range is restarting with 1 after 12 and vise versa.

For example, if one = 2, two = 11 and range = 3, the result is true, as one is the the range [8, 9, 10, 11, 12, 1, 2].

The input has to be 3 seperated integers, the order doesn't matter. Input via string with seperators possible. But the Input has to consist of numbers reading as '10' and not 'A' e.g.

Test cases:

one=7  two=6  range=0   result=false
one=5  two=6  range=1   result=true
one=8  two=11 range=3   result=true
one=1  two=11 range=3   result=true
one=2  two=10 range=3   result=false
one=11 two=1  range=2   result=true
one=6  two=1  range=4   result=false
one=12 two=1  range=3   result=true


Shortest valid answer in bytes wins.

• Welcome to the site! While this challenge is understandable, there are still a few issues. For example, why between 1 and 12, and 0 and 4 particularly? Also, please keep in mind that homework questions are not on-topic here, so if this is homework (or similar), I'd recommend loosening the requirements of the challenge (allowing the integers/range to be any number, etc.). Finally, what do you mean by "Restarting with 1 after 12 and vise versa."? Aug 15, 2019 at 13:29
• @cairdcoinheringaahing Hey thank you, I picked those numbers randomly, but two has to be bigger than one. How big the range is does not really matter. With restarting I mean that your range overlaps over two so if one is 2 and two is 11 and the range is 3, one is still inside the range because it restarts at two with one
– Tom
Aug 15, 2019 at 13:40
• So if one is 2, two is 11, and range is 3, is the task basically to check if one is in the list [8, 9, 10, 11, 12, 1, 2]? That the range cycles round if it goes out of bounds? Aug 15, 2019 at 13:43
• Exactly, sorry If my description was irritating
– Tom
Aug 15, 2019 at 13:45
• one and two really should be called A and B for the sake of clarity. Also, it's still unclear IMO what is given as input and what is a constant. Are $1$ and $12$ the input values, or are the input values guaranteed to be in $[1..12]$? Aug 15, 2019 at 14:34

# Jelly, 108 10 bytes

ŒR’+%12‘Ɠe


Try it online!

Saved 2 bytes thanks to Mr. Xcoder

+2 bytes due a bug if one = 12

I believe this meets the criteria. Takes one from STDIN, range as left argument and two as right argument

• ŒR+%12Ɠe (ŒR is (inclusive) range from $-|z|$ to $|z|$) saves 2 bytes. Takes one from STDIN, range as the first argument and two as the second argument. Try it online! Aug 15, 2019 at 14:00
• @Mr.Xcoder So it does! Thanks Aug 15, 2019 at 14:02
• Your solution has the same problem as my 05AB1E answer had: it fails if one is 12 (i.e. one=12; two=1; range=3 should result in truthy). Aug 15, 2019 at 14:47
• @KevinCruijssen I believe that is fixed, but I'm on mobile so haven't fully tested it Aug 15, 2019 at 14:53

# JavaScript (ES6), 38 bytes

(a,b,r)=>((a=a<b?b-a:a-b)>6?12-a:a)<=r


Try it online!

# JavaScript (Node.js), 53 bytes

Silly branchless method. Expects BigInts. Returns $$\0\text{n}\$$ or $$\1\text{n}\$$.

(a,b,r)=>0x4C5C038B885n>>((a-=b)*a+347n*r)%282n%46n&1n


Try it online!

# Python 2, 52 bytes

lambda a,b,c:a in[i%12or 12for i in range(b-c,b-~c)]


Try it online!

# Python 2, 52 bytes

lambda a,b,c:any(set(range(b-c,b-~c))&{a,a-12,a+12})


Try it online!

# Python 3, 50 bytes

lambda a,b,c:any({*range(b-c,b-~c)}&{a,a-12,a+12})


Try it online!

# 05AB1E, 118 7 bytes

α6Ýûsè@


-3 bytes by porting @Arnauld's JavaScript answer, so make sure to upvote him!
-1 byte thanks to @Grimy.

Inputs in the same order as the challenge description: one; two; range.

Explanation:

α        # Take the absolute difference between the first two (implicit) inputs one & two
6Ý      # Push the list [0,1,2,3,4,5,6]
û     # Palindromize it: [0,1,2,3,4,5,6,5,4,3,2,1,0]
sè   # Swap to get the earlier number again, and use it to index into the list
@  # Then check if the (implicit) input range is larger than or equal to this value
# (after which the result is output implicitly)

• D12α‚ß could be 6Ýûsè. ...what do you mean i just rewrote 75% of your answer? Aug 19, 2019 at 14:52
• "what do you mean i just rewrote 75% of your answer" hehe xD Nice approach though, thanks! Aug 19, 2019 at 15:36

# Java 10, 70 38 bytes

(o,t,r)->((o=o<t?t-o:o-t)>6?12-o:o)<=r


-32 bytes by porting @Arnauld's JavaScript answer.

Try it online.

Explanation:

(o,t,r)->                  // Method with three integer parameters and boolean return-type
((o=o<t?t-o:o-t)          //  Replace o with abs(t-o)
>6?                     //   And if it's smaller than 6:
12-o                 //    Use 12-o
:                     //   Else (it's larger than or equal to 6):
o                    //    Use o itself
)<=r                      //  And check if that is smaller than or equal to r


# Stax, 7 bytes

₧╝g╙╖²╞


Run and debug it

It takes input as space separated [range] [one] [two] and outputs 0 for false, 1 for true.

In pseudo-code:

d = abs(one - two)
return min(d, 12 - d) <= range


Hm, that's probably almost a python submission.

# Python 2, 31 bytes

lambda o,t,r:5-r<(o-t+6)%12<7+r


Try it online!

Python 2, 33 bytes

lambda o,t,r:abs((o-t+6)%12-6)<=r


Try it online!

• Would it be possible to brute-force a solution smaller than this? There are only 720 input combinations. Dec 21, 2020 at 20:19
• @mbomb007 I think it would be hard. The expression is 18 bytes, which is longer than things I've brute-force before which capped out at ~11 bytes. So while there's relatively few inputs, the number of expressions to check would be very large, even limiting to operations already in the answer. Maybe it's doable with some template. Do you have something in mind?
– xnor
Dec 21, 2020 at 22:43
• Not really. I just thought that since there are only 720 cases to account for, maybe something like a K-map could be used to simplify or find a nice solution. However, K-maps are typically for boolean expressions. Dec 21, 2020 at 22:46

# Julia 1.0, 35 bytes

f(o,t,r)=o [email protected](t-1+(-r:r),12)+1


Try it online!

# JavaScript, 37 bytes

r=>b=>g=a=>a-12<b-r?g(a+12):a-12<=b+r


Try it online!

• Write +12 3 times, but I cannot save more bytes...
– tsh
Aug 16, 2019 at 10:04

# Wolfram Language (Mathematica), 24 bytes

Abs@Mod[#-#2,12,-6]<=#3&


Try it online!

t#r=(elem[1+(n-1)mod12|n<-[t-r..t+r]])


two # range returns a function which when applied to one returns a Bool.

Try it online!

# Japt, 11 bytes

aV
aC mU §W


Try it

# Ruby, 45 characters

->o,t,r{[*1..12].rotate(t-r-1)[0..r*2].any?o}


Nothing fancy, just a chance to use Array#rotate.

Sample run:

irb(main):001:0> ->o,t,r{[*1..12].rotate(t-r-1)[0..r*2].any?o}[2, 11, 3]
=> true


Try it online!

o%t=(abs(mod(o-t+6)12-6)<=)


Try it online!

# Charcoal, 11 bytes

‹⁵⁺Ｎ↔⁻⁶↔⁻ＮＮ


Try it online! Link is to verbose version of code. Takes the size of the range as the first input. Output uses Charcoal's default Boolean format of - for true, nothing for false. Explanation:

         ＮＮ Two inputs
↔⁻   Absolute difference
⁶     Literal 6
↔⁻      Absolute difference
⁺Ｎ        Plus range
‹⁵          Is greater than 5


# R, 48 bytes

function(a,b,r){a%in%c(9:12,1:12,1:4)[-r:r+b+4]}


Try it online!

Pretty straightforward, I struggled for a while to find the best way to test if a was in the given range. There is likely a method I haven't seen or tried yet.

# C# (Visual C# Interactive Compiler), 78 bytes80 bytes 72 bytes

(a,b,r)=>Enumerable.Range(b-r,2*r+1).Select(x=>(x+12)%12).Contains(a%12)


Try it online!

Edit: Range was incorrect, I forgot to count the item itself, adding 2 bytes. But I bit the bullet dealing with C#'s modulus actually being remainder, saving 8 bytes. Technically, this code actually treats 12 as 0, but this is still valid in this context.

# C#, 158 bytes

using System;public class P{public static void Main(string[]a){int w=Math.Abs(int.Parse(a[0])-int.Parse(a[1]));Console.Write((w>6?12-w:w)<=int.Parse(a[2]));}}


Try Online

# MathGolf, 7 bytes

-±_B,╓≥


Try it online!

Other valid 7-byters include:

-±7rñ§≥
-7rñ╡§≥


I found a 4-byter which solves every test case, but doesn't solve the problem in general:

-±Σ≥


Try it online!

The approach of the 7-byters are basically ports of existing 7-byte solutions. The 4-byter relies on the fact that the digit sum of the absolute difference of the first two is less than the third input for all test cases. That correlates with the desired output for the test cases, but not in general.

# Perl 5, 63 bytes

sub f{grep$_[0]==$_%12,(1..36)[11+$_[1]-$_[2]..11+$_[1]+$_[2]]}


Try it online!

Ungolfed:

sub f {
my($one,$two, $range) = @_; my @n = (1..36); return 0 + grep$one==$_%12, @n[ 11+$two-$range .. 11+$two+\$range ];
}