28
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
Sandbox – Stephen – 2017-08-09T15:15:31.950
13Holy crap! Pressing the two buttons does reset my (European) alarm clock to 0:00 as well... All these years spent pressing the buttons too many times... O_o – Arnauld – 2017-08-09T15:29:26.620
@StepHen, I know you're very relaxed on the input format here, but could you throw an eye at my JS solution, let me know if it's OK to take input like that? – Shaggy – 2017-08-09T15:30:28.663
@Arnauld I discovered it by chance as well - since then I've done these mental gymnastics in my head to figure out which was optimal when trying to reset my alarm :P – Stephen – 2017-08-09T15:30:36.163
@Shaggy yup that's fine. Same thing that HyperNeutrino's Python answer is doing – Stephen – 2017-08-09T15:31:10.643
8"military time (or regular time for most of the world)"...the term you're looking for is "24-hour time". – Jakob – 2017-08-09T16:08:55.833
@Jakob I know that's the term, but (unless I'm mistaken) very few people would call it that casually, as if you use it it's just regular time - I may be mistaken though. – Stephen – 2017-08-09T16:10:56.887
12@Jakob No, the term he's looking for is "regular time". Americans use irregular time, irregular dates, irregular units, etc. etc. – Neil – 2017-08-09T20:49:58.760
1@StepHen I am in the UK and had no idea what you meant by "military time" until Jakob explained it. 24hr time makes perfect sense to me – Darren H – 2017-08-10T05:43:59.573