[Question]
  [
# My Alarm Clock
I'm American, and so is my (digital) alarm clock. To set the alarm, it starts at the time it was previously. Hitting the hour button moves it up one hour, and hitting the minute button moves it up one minute. Hitting both buttons at the same time resets it to midnight (12:00 am), and counts as two button presses.
When the hours exceed their cap (12), it resets to 1 and toggles the AM/PM light. When the minutes exceed their cap (59), they reset to 0, without affecting the hours.
# The Task
Your task is, given a starting time and a target time, to output the optimal number of buttons presses needed to set my alarm to the target time.
You may take input in whatever format suits you best. The only data your program should need is hours and minutes, for both inputs. That means that you, for example, may take data as milliseconds since epoch, and extract the hours and minutes, but you may not encode *anything* into the year, month, second, etc. Note that while you may, for example, input using "military time" (or regular time for most of the world), but that doesn't change how my clock works.
# Examples
```
1:15 pm -> 2:30 am
```
You could push down both buttons to reset to 12:00 am, then increment to 2:30 am, which would be `2+2+30 = 34` button presses. You could also increment to 2:30 am, which would be `13+15 = 28` button presses. Therefore, your output is `28`.
```
3:58 am -> 4:02 am
```
You could reset and increment, which would be `2+4+2 = 8` button presses. You could also increment, which would be `1+4 = 5` button presses. Therefore, your output is `5`.
```
10:55 pm -> 1:00 am
```
You could reset and increment, which would be `2+1 = 3` button presses. You could also increment, which would be `3+5=8` button presses. Therefore, your output is `3`.
```
1:00 am -> 1:59 pm
```
You could reset and increment, but that would be three more presses than just incrementing. Therefore, your output is `12+59 =`  `71`.
# Test Cases
```
Current  Target   = Output
1:15pm   2:30am   = 28
3:58am   4:02am   = 5
10:55pm  1:00am   = 3
1:00am   1:59pm   = 71
12:00am  12:00am  = 0
6:51pm   12:00am  = 2
2:01pm   11:00pm  = 25
2:01pm   11:01pm  = 9
12:59am  12:01am  = 2
11:50am  12:00pm  = 11
```
  
[Answer]
# 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](https://codegolf.stackexchange.com/users/58563/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.
[Answer]
# [Pyth](https://pyth.readthedocs.io), 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.](http://pyth.herokuapp.com/?code=hS%2C%2B%25-eQ%40Q1+60%25-%40Q2hQ24%2B2s%3EQ2&input=13+15+02+30%0A03+58+04+02%0A22+55+01+00%0A01+00+13+59%0A00+00+00+00%0A18+51+00+00%0A14+01+23+00%0A14+01+23+01%0A00+59+00+01%0A11+50+12+00&test_suite=1&test_suite_input=%5B13%2C+15%2C+2%2C+30%5D%0A%5B3%2C+58%2C+4%2C+2%5D%0A%5B22%2C+55%2C+1%2C+0%5D%0A%5B1%2C+0%2C+13%2C+59%5D%0A%5B0%2C+0%2C+0%2C+0%5D%0A%5B18%2C+51%2C+0%2C+0%5D%0A%5B14%2C+1%2C+23%2C+0%5D%0A%5B14%2C+1%2C+23%2C+1%5D%0A%5B0%2C+59%2C+0%2C+1%5D%0A%5B11%2C+50%2C+12%2C+0%5D&debug=0)** Numbers in Pyth do not support leading zeros.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 16 bytes
```
§▼ṁ→(Σz%e24 60z-
```
[Try it online!](https://tio.run/##AS8A0P9odXNr///Cp@KWvOG5geKGkijOo3olZTI0IDYwei3///9bMjIsNTVd/1sxLDAwXQ "Husk – Try It Online")
Takes arguments as two lists [hours,minutes], for start and end time, in 24h format.
I'm quite happy about how much I was able to golf this one, I find interesting how arguments are managed in this composition of functions.
The function which calculates how many key presses we need if reset is not allowed is the following:
```
Σz%e24 60z-
         z-    elementwise subtract the two times
 z%e24 60      take the hours difference modulo 24 and the minutes difference modulo 60
Σ              sum the two resulting numbers
```
The interesting part is that since the rest of this solution can work only with a single list as argument, this one gets partially applied to the first argument of the whole program, "eating" it and leaving only the second argument visible both for itself and the rest of the program.
Next, we compute how many key presses we need if we reset the time to 0:00
```
ṁ→    take the sum of each element of the list increased by 1
```
As said before, this operates only on the second argument (the final time), and computes `hours+minutes+2`, just in a golfier way.
Finally, `§▼` is the part that passes the second argument to both functions, and returns the lesser of the two results.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 19 bytes
```
_⁵%24µ+⁴_⁶%60µ«³+⁴¤
```
[Try it online!](https://tio.run/##y0rNyan8/z/@UeNWVSOTQ1u1HzVuAXK2qZoZHNp6aPWhzSCBQ0v@//9v9N/Y4L@h8X9DUwA "Jelly – Try It Online")
Input as 4 integers (end-hour, end-minute, start-hour, start-minute)
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 56 bytes
```
(H,M,h,m)=>Math.Min(((h+24-H)%24)+((m+60-M)%60),(2+h+m))
```
[Try it online!](https://tio.run/##XY2xCoMwFEX3fEUW4T0SxYq4WIVSKA7N1KFzSEMNmERMLBTpt9s6FEo53LNdjgqp8pNe52DcnV6eIWpbEyetDqNUmrqFqEGGQMeFhCijUfQ0O7U3LvK/tdSP0Vg5HAY5Wdqs0HHBe26xaYWMfSaMA4CeFWXaYVKUyAAsq/JUYFLlyKFgPbOIa/1NPby5USE/P1zI0bvgB51dJxP12TgNvz3YFTznmxBr8tpY3w "C# (.NET Core) – Try It Online")
Very similar to the Javascript answer. Bools in C# don't convert easily to numbers, so instead of `[diff]+24*(H<h)` I did `([diff]+24)%24` which effectively does the same thing.
[Answer]
# Haskell, 41 bytes
```
(a#b)c d=min(a+b+2)$mod(a-c)24+mod(b-d)60
```
Pretty straightforward. Takes input as four arguments using 24-hour time: end hour, end minute, start hour, start minute.
[Answer]
# [Python 3](https://docs.python.org/3/), 43 bytes
```
lambda a,b,c,d:min((c-a)%24+(d-b)%60,2+c+d)
```
[Try it online!](https://tio.run/##VYzNDoIwDMfP7il6IelkmHVjBkh8Ey8DRJfAXBAPPj0WEw42Tf8f@aXpszye0a7D5bqOfmp7D161qlN9M4WI2BVeZqbMsS9amZ21MnmX93IdnjMECBFmH@83JC0bcUhziAsOeBzDa8HJJ@SsmErvBeXplcbAus1KFsiBNmC10BZcBbrkKIwBxz2B5n67wKSrBZt9BVXgaPflBhv772njXf1jSBCB4z@G4xc "Python 3 – Try It Online")
Input as 4 integers (start-hour, start-minute, end-hour, end-minute)
[Answer]
# Java 8, ~~54~~ 50 bytes
```
(h,m,H,M)->Math.min((H-h+24)%24+(M-m+60)%60,H+M+2)
```
Port of [*@KamilDrakari*'s C# answer](https://codegolf.stackexchange.com/a/138293/52210) (after I golfed ~~2~~ 6 bytes).
**Explanation:**
[Try it here.](https://tio.run/##jZKxasMwEIb3PsUtAQnLjiRbaUxoZy/OkrF0UB23dhrLJlYCpeTZ3ZPtQOggAroDSff995/QQV902HalOey/h@Ko@x5yXZvfJ4Da2PL0qYsStm47HkBBXK6Yy82YszHndIM1VwxcvdW2LmALBl5gIBVrWMZyGr7m2lZRUxtCsrAKZEIXMglIHjbBitPFirMsyANJh82k050/jqgzy13aeg8NmiM7e6rN19s7aDo52/30tmyi9myjDq/s0RATFUTEDIRiwCWDmFO6geUS5NqHcETUGpHEYTOifIREceWaCIxbk9jbY6xEa65XOhPPwovwCZliRrh3dhxCiX@E9BLJNISM7wn1MCJmJPUS7rVSnF3eEX5bqK/4TNxsCTH@tuvwBw)
```
(h,m,H,M)->       // Method with four integer parameter and integer return-type
  Math.min(       //  Return the lowest of the following two:
                  //   The hours:
   (H-h           //    Second hours - first hours inputs
       +24)       //    +24 so we won't have negative numbers
           %24    //    mod-24 to get the hours
   +              //   And add the minutes:
    (M-m          //    Second minutes - first minutes inputs
         +60)     //    +60 so we won't have negative numbers
             %60  //    mod-60 to get the minutes
    ,H+M          //  And compare it to the second hours + second minutes inputs
        +2)       //   +2 for the added +24 and +60 to get the positive modulo arguments
```
[Answer]
# [Perl 5](https://www.perl.org/), 71 +2 (-ap) = 73 bytes
```
($w,$x,$y,$z)=@F;$_=($r=2+$y+$z)>($c=($y-$w+24)%24+($z-$x+60)%60)?$c:$r
```
[Try it online!](https://tio.run/##DcixCsIwFAXQX8lwCwkvkZfQOLSkOrn5DSLFQRAbotCmH@8zw1lOfpRXFNFYLTaLarGbdL6MuCWNkgKhUqtJY25RHVYKvelCTxq7w0ZHNl1zwjygiHivIisfFPNvyd/n8v6Iu8YDexZ3z38 "Perl 5 – Try It Online")
Takes input in 24 hour format (military time), separated by spaces, start time first, end time second: *HH MM hh mm*
[Answer]
# [Retina](https://github.com/m-ender/retina), 106 bytes
```
\d+
$*
 (1*):(1*)
 24$*1$1:60$*1$2#11$1$2
(1*):(1* )\1(1{24})?
$2
(1*) (1*):\1(1{60})?
$2
(1+)#(?!\1)
\G1
```
[Try it online!](https://tio.run/##VYwxDsIwDEV3nyKoQUraxd9pqtZLx14iA0gwsDAgtoqzh6RQJJbv76dnP67P2/2cj2455XTpyLZkHFqvNchIb1tY6MB1SoOyWKHdMD7BYZX@5Wf68s/5xgf@8c43bj4keKK0IGcERTQsGpg4aBwN98pCIhoLh3LhNU0x40TMtW9JGDVi7@UKRsJ/R/XjtDkgQGP5I8V5Aw "Retina – Try It Online") Link includes test cases. Takes input as current and desired times in regular 24 hour time with a space separating the two times. Explanation:
```
\d+
$*
```
Convert to unary.
```
 (1*):(1*)
 24$*1$1:60$*1$2#11$1$2
```
This does two things; it adds 24 hours and 60 minutes to the desired hours and minutes, and it also adds 2 to the sum of the original desired hours and minutes, i.e. the the number of button presses to set using a reset.
```
(1*):(1* )\1(1{24})?
$2
```
Subtract the current hours from the desired hours, and subtract the 24 we added on if we can.
```
(1*) (1*):\1(1{60})?
$2
```
Similarly for the minutes. This also adds the two results together.
```
(1+)#(?!\1)
```
If the number of presses to set using the current time is more than the number of presses to set using a reset, delete it.
```
\G1
```
Convert the first remaining number back to decimal.
]