23
1
An analog clock has 2 hands*: Hour and minute.
These hands circle the clock's face as time goes by. Each full rotation of the minute hand results in 1/12th of a rotation of the hour hand. 2 full rotations of the hour hand signifies a full day.
As these hands are fixed to the same central point, and rotate around that point, you can always calculate the angle between the hands. In fact there are 2 angles at any given time; A larger one, and a smaller one (sometimes they will both equal 180, but that's not important)
*Our hypothetical clocks don't have second hands
Task
Given a time in 24 hour format, output the smaller angle between the hands, in degrees. If the hands are directly opposite eachother (such as at 6:00
, 18:00
etc) output 180
Rules
Input may be taken as:
- A delimiter separated string: 6:32
, 14.26
- 2 separate values, strings or ints: 6, 32
, 14, 26
- An array of 2 values, strings or ints: [6, 32]
, [14, 26]
You may also optionally specify that your answer requires inputs be padded to 2 digits (assuming you take strings), ie: 06:32
, 06, 32
, [06, 32]
You may also optionally reverse the order of the inputs, taking minute then hour, ie: 32:6
, 32, 6
, [26, 14]
Hour will be an integer value between 0
and 23
(inclusive)
Minute will be an integer value between 0
and 59
(inclusive)
You can assume that the minute hand snaps to increments of 6 degrees along the face (one evenly-spaced position for each minute value)
You can assume that the hour hand snaps to increments of 0.5 degrees along the face (one evenly-spaced position for each minute value per hour value)
Output must be given in degrees, not radians. You may include a trailing .0
for whole numbers
Scoring
This is code-golf so fewest bytes in each language wins!
Testcases
Input: 06:32
Output: 4
Input: 06:30
Output: 15
Input: 18:32
Output: 4
Input: 06:01
Output: 174.5
Input: 00:00
Output: 0
Input: 00:01
Output: 5.5
Input: 12:30
Output: 165
Input: 6:00
Output: 180
Input: 23:59
Output: 5.5
Sandbox post – Skidsdev – 2019-06-21T17:06:16.210
2@FryAmTheEggman "Output must be given in degrees, not radians", so I would guess not – Theo – 2019-06-21T17:27:34.890
1forgot that at 5:59 hour hand is almost at 6 – aaaaa says reinstate Monica – 2019-06-21T17:32:21.917
I was seriously considering posting something similar, except that the time would include the seconds, and that both hour and minute hands would snap to the nearest whole degree (because that's how my watch works, ignoring that it has a second hand). – Neil – 2019-06-21T18:54:01.557
4Suggested test case:
00:59 -> 35.5
(a small value of $h$ with a large value of $m$ is likely to make some implementations fail). – Arnauld – 2019-06-22T12:37:11.1071Thanks, @Arnauld, you just cost me a byte! :p – Shaggy – 2019-06-22T23:25:14.207
Related: https://codereview.stackexchange.com/a/182054
– polfosol ఠ_ఠ – 2019-06-24T09:13:58.087