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.

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60)
  • 2 bytes saved thanks to Arnauld.

Try it

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

o.innerText=(f=

(g,l,h,m)=>Math.min(2+h+m,(h-g+24)%24+(m-l+60)%60)

)(+(x=(i.value="01:00").split`:`)[0],+x[1],+(y=(j.value="13:59").split`:`)[0],+y[1]);oninput=_=>o.innerText=f(+(x=i.value.split`:`)[0],+x[1],+(y=j.value.split`:`)[0],+y[1])
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>


Explanation

(To be updated shortly.)

(g,l,h,m)=>

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.

2+h+m

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.

h-g+24*(h<g)

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.

+m-l+60*(m<l)

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.

Math.min()

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,+%-eQ@Q1 60%-@Q2hQ24+2s>Q2

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


Jelly, 19 bytes

_⁵%24µ+⁴_⁶%60µ«³+⁴¤

Try it online!

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