Alarm Optimization

JavaScript (ES6), 73 56 54 52 50 bytes

Uses 24 hour format. Takes input as 4 integers representing the hours & minutes of each time.

  • 2 bytes saved thanks to Arnauld.

Try it

Enter the times in 24-hour format, with the : separator.



label,input{font-family:sans-serif;font-size:14px;height:20px;line-height:20px;vertical-align:middle}input{margin:0 5px 0 0;width:100px;}
<label for=i>Current: </label><input id=i type=time><label for=j>Target: </label><input id=j type=time><pre id=o>


(To be updated shortly.)


Anonymous function taking the integers as arguments via parameters g, l, h & m, where g & l are, respectively, the hours & minutes of the current time and h & m are the hours & minutes of the target time.


First, we calculate how many button presses are needed if we just reset the clock, which is simply 2 (for the reset) plus the target hour and the target minute.


Next we calculate how many button presses are needed to reach the target hour. We do this by subtracting the current hour from target hour. However, if the current hour is less than the target, this will give us a negative number so we rectify that by adding 24 multiplied by checking if h<g (which returns a boolean but is implicitly cast to integer 1, if true, or 0 if false by the mathematical operations.


We use a similar formula to calculate the number of presses to get from the current minute to the target minute and add that to the hour presses.


Finally, we get the minimum of the 2 numbers to give us our result.

Pyth, 29 bytes

This challenge obviously doesn't advantage golfing languages, that's why it's so long. On the other hand, this is improved by the fact that Pyth is Python-based, so we can abuse its negative modulus.

hS,+%[email protected] 60%[email protected]+2s>Q2

Test Suite. Numbers in Pyth do not support leading zeros.

Jelly, 19 bytes


Try it online!

Input as 4 integers (end-hour, end-minute, start-hour, start-minute)