9
\$\begingroup\$

You are chained to a chair. Underneath you is a huge volcano. A 12-hour clock next to you ticks ominously, and you see that it has wires leading from the back up to a chain, which will drop you into the center of the earth. Taped to the clock is a note:

Each clock hand has an electrode. When both clock hands are in the same position, the power flows and you die. That is, unless you can tell me the exact time that this will occur, to the nearest minute.

You have a computer that knows every programming language. You need to create the shortest (this is , and standard loopholes are prohibited) program you can, and tell the evil scientist what the time will be. Your program should take input (in any method), consisting of the hour and minute. It should return the next hour and minute (in any method) that this occurs.

According to the OEIS page, the eleven overlap times are:

00:00:00 plus 0/11 s, 01:05:27 plus 3/11 s,
02:10:54 plus 6/11 s, 03:16:21 plus 9/11 s,
04:21:49 plus 1/11 s, 05:27:16 plus 4/11 s,
06:32:43 plus 7/11 s, 07:38:10 plus 10/11 s,
08:43:38 plus 2/11 s, 09:49:05 plus 5/11 s,
10:54:32 plus 8/11 s.

The next time would be 12:00:00. The seconds and their fractional parts are not needed for this challenge. Simply round to the nearest minute.

Test cases:

0:00 (Or 12:00) > 1:05
1:00 > 1:05
11:56 > 12:00 (Or 0:00)
6:45 > 7:38
5:00 > 5:27
6:30 > 6:33 (round up)

The program can be a function, or full program. I do not care if you choose 0:00 or 12:00, and both are acceptable. Good luck!

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Mego Aug 10 '18 at 13:14

11 Answers 11

6
\$\begingroup\$

JavaScript (Node.js), 54 47 bytes (round to the nearest)

-7 bytes. Thanks @user202729

a=>b=>[(a+=b>5.46*a)+a/11|0,a%12*65.46%60+.5|0]

Try it online!


JavaScript (Node.js), 40 33 44 bytes (rounds towards 0)

-3 bytes thanks to @Arnauld

-4 bytes thanks to @Kevin Cruijssen

a=>b=>[(a+=b>5.46*a)+a/11|0,a%12*65.46%60|0]

Explanation

a=>b=>[(a+=b>5.46*a)+a/11|0,a%12*65.46%60|0]    Full Code
a                                               Hours
   b                                            Minutes
    =>[                    ,               ]    return array with
       (a+=        )                            add to the current hour
           b>5.46*a                             1 if the minute's clock hand has 
                                                passed the hour's clock hand. Here we use
                                                equation 60*a/11 which is the same as 5.46*a
                    +a/11                       and add 1 when hour is 11
                         |0                     floor the result

                            a%12*65.46%60|0     Here we do equation ((720/11)*a) (mod 60)
                            a%12                In case of hour 12 we take 0
                                *65.46          multiply hour by 720/11 which can be shortened to
                                                65.46 to save 1 byte.
                                      %60       mod 60
                                         |0     floor the result

Side note: I'm pretty sure this can be golf down by someone with more knowledge at math. I barely know how to sum and multiply

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This doesn't look right if you take how a real clock works into action: datagenetics.com/blog/november12016/index.html \$\endgroup\$ – Night2 Aug 2 '18 at 14:18
  • \$\begingroup\$ You have some rounding errors. 05:00 should output 05:27 but outputs 05:25 instead, and 06:45 should output 07:38 but outputs 07:35 instead. Here perhaps a useful oeis sequence: A178181 \$\endgroup\$ – Kevin Cruijssen Aug 2 '18 at 14:30
  • 1
    \$\begingroup\$ @LuisfelipeDejesusMunoz The two test cases I gave are indeed correct now, but your 11:56 seems to output 00:05 instead of 00:00 (or 12:00). \$\endgroup\$ – Kevin Cruijssen Aug 2 '18 at 14:50
  • \$\begingroup\$ @KevinCruijssen Done. I think a=(a+=b>=a*5)%12 can be shorten a little bit but i'm not too good at this \$\endgroup\$ – Luis felipe De jesus Munoz Aug 2 '18 at 15:03
  • 1
    \$\begingroup\$ Isn't Math.round(x) just 0|x+.5? \$\endgroup\$ – user202729 Aug 3 '18 at 2:43
5
\$\begingroup\$

J, 31 bytes

0.5<.@+>:@<.&.(11r720*12 60&#.)

Try it online!

The way to round a number in J is to add 0.5 and take the floor (<.). Takes too many bytes...


Explanation

12 60&#. (mixed base conversion) converts from an array of [hour, minute] to the minute passed since 0:00.

Note that starting from 0:00, every 12/11 hours (that is, 720/11 minutes), the two hands overlap once.

Therefore, given the minute value, just round it up to the nearest multiple of 720/11 (different from itself). This can be achieved by * it by 11/720 (J has rational number literal 11r720), take the floor <., increment >:, then multiply it by 720/11.

Note that "multiply by 11/720" and "multiply by 720/11" are 2 reverse action, as well as "convert from [hour, minute] to number of minute passed" and vice versa. Fortunately J has built-in &. (under), which reverses some action after applying a transformation.

After that just do the rounding: 0.5 + then <..

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

R, 68 bytes

a=round(1:30*720/11);a[a>sum(scan()*c(60,1))][1]%/%c(60,1)%%c(12,60)

Try it online!

  • -2 bytes thanks to Giuseppe
  • +7 bytes due to missing rounding :(

Exploiting the equation :

same_position_minutes = 720/11 * index

where index is 0 for the first overlapping position (00:00), 1 for the 2nd and so on...

\$\endgroup\$
  • 1
    \$\begingroup\$ I think you have an extraneous set of parentheses around (a[...]...c(60,1)) \$\endgroup\$ – Giuseppe Aug 2 '18 at 17:43
  • \$\begingroup\$ @Giuseppe: yep, you're right... thanks ! \$\endgroup\$ – digEmAll Aug 2 '18 at 20:29
  • \$\begingroup\$ @digEmAll This gives the wrong answer for 6:30 > 6:33. \$\endgroup\$ – mbomb007 Aug 2 '18 at 20:45
  • \$\begingroup\$ @mbomb007 : you're right, fixed ;) \$\endgroup\$ – digEmAll Aug 3 '18 at 7:02
4
\$\begingroup\$

R, 88 bytes

i=scan();d=1+60*i[1]+i[2];while(abs(60*(h=d%/%60%%12)-11*(m=d%%60))>5){d=d+1};paste(h,m)

Try it online!

Increase time by one minute. Checks the angle. If not close enough, loops until a solution is found.

\$\endgroup\$
  • 1
    \$\begingroup\$ Huh, that's a cool way of doing it. I never thought of simulating an actual clock! \$\endgroup\$ – Redwolf Programs Aug 3 '18 at 1:44
3
\$\begingroup\$

Python 3, 80 78 bytes

This is my first submission, so constructive criticism is welcome :)

-2 bytes thanks to @Jo King

def f(h,m):n=65.45;r=round(((60*h+m)//n%11+1)*n);print('%i:%02i'%(r//60,r%60))

Try it online! (78) Try it online! (80)

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to PPCG! Good first answer! \$\endgroup\$ – mbomb007 Aug 3 '18 at 14:06
2
\$\begingroup\$

Java 8, 89 82 bytes

(h,m)->(m=m<(m=(int)(h%12*720d/11%60))?m:(int)(++h%12*720d/11%60))*0+h%12%11+" "+m

Fixed. Will see if I can golf it later (probably by porting another answer)..

Try it online.

Explanation:

TODO

\$\endgroup\$
  • \$\begingroup\$ Same issue (obviously) as the answer you ported - e.g.: f.apply(56).apply(10) yields 11 59 \$\endgroup\$ – Jonathan Allan Aug 2 '18 at 17:52
  • \$\begingroup\$ @JonathanAllan Fixed. Will see if I can remove some bytes later.. \$\endgroup\$ – Kevin Cruijssen Aug 2 '18 at 20:05
  • \$\begingroup\$ @KevinCruijssen This gives the wrong answer for 6:30 > 6:33. \$\endgroup\$ – mbomb007 Aug 2 '18 at 20:46
  • \$\begingroup\$ @mbomb007 I know. I'm waiting for OP's response before fixing that. Whether we should floor, round, ceil, or both are allowed (if I would have posted the challenge I would use the fourth option, but let's wait for OP first). \$\endgroup\$ – Kevin Cruijssen Aug 2 '18 at 20:50
  • \$\begingroup\$ @KevinCruijssen The test case was edited into the question because of a comment by the OP. Using the most common definition of round, the OP's intent is clear. \$\endgroup\$ – mbomb007 Aug 2 '18 at 20:53
2
\$\begingroup\$

Apl (Dyalog Unicode), 28 bytes

((⍳11),⍪0,+\∊5/⊂5 6)(⍸⌷1⊖⊣)⎕

Try it online!


Explanation

((⍳11),⍪0,+\∊5/⊂5 6) is a matrix of times where the hands overlap (printed at the end of the tio link)
(⍸⌷1⊖⊣)⎕ finds the interval in which the input is in the matrix and indexes below it wrapping around.

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

C# (.NET Core), 70 bytes

(h,m)=>{h%=12;int n=(5*h+h/2)%60;return (m>n||h>10)?f(h+1,0):h+":"+n;}

Try it online!

I think it passes all test cases. Although the h=11 case is kind of ugly

Explanation:

(h,m)=>{ // Lambda receiving 2 integers
    h%=12; // Just to get rid of the 0/12 case
    int n=(5*h+h/2)%60; // get the minute at which the hands overlap 
                        //for current hour.
    return 
    (m>n||h>10)? // if current minute > n or h=11
        f(h+1,0) // it will happen next hour
    :
        h+":"+n; // return result
}
\$\endgroup\$
  • \$\begingroup\$ This gives the wrong answer for 6:30 > 6:33. \$\endgroup\$ – mbomb007 Aug 2 '18 at 20:47
  • \$\begingroup\$ @mbomb007 Thanks, I'm going to have a look on this. I did the first try before the complete list of overlap times was added. \$\endgroup\$ – F.Carette Aug 2 '18 at 21:05
  • \$\begingroup\$ It should be ok now. Since we don't have a clear instruction about what to do in cases where currentTime == overlapTime, I return the current time in these cases (given (1,5) return "1:5" and not "2:11"). \$\endgroup\$ – F.Carette Aug 3 '18 at 7:08
1
\$\begingroup\$

JavaScript, 41 bytes

p=>q=>(p+=q>=(5.5*p|0),p%=11,[p,5.5*p|0])

f =
p=>q=>(p+=q>=(5.5*p|0),p%=11,[p,5.5*p|0])
<div oninput="o.value = f(+h.value)(+m.value).join(':')">
Input: <input type=number min=0 max=11 id=h style=width:40px value=0 autofocus>:<input type=number min=0 max=59 id=m style=width:40px value=0><br />
Output: <output id=o></output>
</div>

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

Jelly, 25 bytes

‘2¦ɓ;W}Ṣi¹ịḷø5,6ẋ5ÄĖØ0W¤;

Try it online!

A monadic link that takes the time as a two-integer list and returns a two-integer list corresponding to the next time the hands are due to touch.

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

Perl 6, 43 bytes

(* *60+*+33).round(65.45).round.polymod(60)

Try it online!

An anonymous Whatever lambda that takes two integers representing hours and minutes and returns the hours and minutes in reverse order. Right now it isn't consistent when you input an aligned time, whether it outputs the next aligned time or stays on the same one. I'm waiting on OP to respond on that matter, but right now I'm treating it as undefined.

Explanation

(* *60+*+33)   # Converts the two inputs to number of minutes
            .round(65.45)   # Round to the nearest multiple of 65.45
                         .round  # Round to the nearest integer
                               .polymod(60) # Repeatedly modulo by 60 and return the list of results
\$\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.