Given a 24 hour time, e.g. 20:48, output the nearest time where each adjacent pair of digits has an absolute difference of 1, e.g. 21:01.
That is, each number in the output must differ from the numbers next to it by 1, and the output must be the closest time, forwards or backwards, for which this is true. Time can be considered to wrap around. For example, 23:55 and 00:05 are only 10 minutes apart.
Input
- Input can be in any reasonable form which represents all four digits of a 24 hour time. E.x. a string, "20:48" (colon optional), a list of integers, [2, 0, 4, 8], or a single integer 2048.
- Leading zeros are optional for input. Use whatever is most convenient.
- You can assume the input will always represent a valid 24 hour time.
Output
- You must output four integers which represent a valid 24 hour time. The individual integers can be represented in whatever form is most convenient. E.x. [2, 1, 0, 1] or "2101".
- Leading zeros are not optional for output. E.x. you can't output 1:23 instead of 01:23.
- You may optionally output a separator between the hours and minutes. This can be a colon, period, space, newline, or some other junk that saves you a byte. Whatever separator you choose, it must be consistent from one output to another.
- If there are two equally close times to the input which satisfy the requirements, you may output either.
- Note there are only 16 possible outputs: 01:01, 01:21, 01:23, 10:10, 10:12, 12:10, 12:12, 12:32, 12:34, 21:01, 21:21, 21:23, 23:21, 23:23, 23:43, and 23:45.
Test cases
- 00:10 -> 23:45
- 01:23 -> 01:23
- 05:46 -> 01:23
- 05:47 -> 10:10
- 12:00 -> 12:10
- 16:47 -> 12:34
- 16:48 -> 21:01
- 20:48 -> 21:01
- 21:22 -> 21:23 or 21:21
Python 3 reference implementation.
Score
This is code golf, so the shortest program in bytes wins. The standard loopholes are forbidden.
05:46 -> 01:23
,05:47 -> 10:10
,16:47 -> 12:34
, and16:48 -> 21:01
to the tests since these are the midpoints of the widest intervals. \$\endgroup\$