2
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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."? \$\endgroup\$ – caird coinheringaahing Aug 15 at 13:29
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – Tom Unger Aug 15 at 13:40
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – caird coinheringaahing Aug 15 at 13:43
  • \$\begingroup\$ Exactly, sorry If my description was irritating \$\endgroup\$ – Tom Unger Aug 15 at 13:45
  • 6
    \$\begingroup\$ 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]\$? \$\endgroup\$ – Arnauld Aug 15 at 14:34

20 Answers 20

2
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 10 8 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

\$\endgroup\$
  • \$\begingroup\$ Œ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! \$\endgroup\$ – Mr. Xcoder Aug 15 at 14:00
  • \$\begingroup\$ @Mr.Xcoder So it does! Thanks \$\endgroup\$ – caird coinheringaahing Aug 15 at 14:02
  • \$\begingroup\$ 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). \$\endgroup\$ – Kevin Cruijssen Aug 15 at 14:47
  • 1
    \$\begingroup\$ @KevinCruijssen I believe that is fixed, but I'm on mobile so haven't fully tested it \$\endgroup\$ – caird coinheringaahing Aug 15 at 14:53
4
\$\begingroup\$

05AB1E, 11 8 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.

Try it online or verify all test cases.

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ D12α‚ß could be 6Ýûsè. ...what do you mean i just rewrote 75% of your answer? \$\endgroup\$ – Grimy Aug 19 at 14:52
  • 1
    \$\begingroup\$ "what do you mean i just rewrote 75% of your answer" hehe xD Nice approach though, thanks! \$\endgroup\$ – Kevin Cruijssen Aug 19 at 15:36
3
\$\begingroup\$

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

Julia 1.0, 35 bytes

f(o,t,r)=o in@.mod(t-1+(-r:r),12)+1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript, 37 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Write +12 3 times, but I cannot save more bytes... \$\endgroup\$ – tsh Aug 16 at 10:04
1
\$\begingroup\$

Wolfram Language (Mathematica), 24 bytes

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

Try it online!

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

Haskell, 42 bytes

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

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

Try it online!

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

Japt, 11 bytes

aV
aC mU §W

Try it

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

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!

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

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!

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

Haskell, 27 bytes

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

Try it online!

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

Charcoal, 11 bytes

‹⁵⁺N↔⁻⁶↔⁻NN

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:

         NN Two inputs
       ↔⁻   Absolute difference
      ⁶     Literal 6
    ↔⁻      Absolute difference
  ⁺N        Plus range
‹⁵          Is greater than 5
\$\endgroup\$
1
\$\begingroup\$

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.

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

C# (Visual C# Interactive Compiler), 78 bytes 80 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.

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

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

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

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.

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

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 ];
}
\$\endgroup\$

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.