29
1
Originally posted (and deleted) by @Tlink, which was most likely inspired from this StackOverflow question.
Since it was a shame it got deleted, because it seemed like a good challenge in general, I figured I'd repost it with proper formatting and rules. (I've tried contacting @Tlink and get his/her permission to post it, but (s)he doesn't respond any more, which is why I decided to post it myself now.)
Input: Six digits.
Output: Either the first or last valid time in the 24-hour format (00:00:00
through 23:59:59
). (You can choose yourself whether you output the first or last valid time.)
Example:
When the inputs are 1,8,3,2,6,4
, the following times can be created:
12:36:48 12:38:46 12:46:38 12:48:36
13:26:48 13:28:46 13:46:28 13:48:26
14:26:38 14:28:36 14:36:28 14:38:26
16:23:48 16:24:38 16:28:34 16:28:43
16:32:48 16:34:28 16:38:24 16:38:42
16:42:38 16:43:28 16:48:23 16:48:32
18:23:46 18:24:36 18:26:34 18:26:43
18:32:46 18:34:26 18:36:24 18:36:42
18:42:36 18:43:26 18:46:23 18:46:32
21:36:48 21:38:46 21:46:38 21:48:36
23:16:48 23:48:16
So we'll output either 12:36:48
or 23:48:16
in this case, being the first / last respectively.
Challenge rules:
- State whether you output the first or last valid time in your answer.
- I/O is flexible. Input can be six separated integers; a string containing the six digits; an integer list/array; a single (possibly octal) number; etc. Output can be a correctly ordered list/array of digits; a String in the format
HH:mm:ss
/HHmmss
/HH mm ss
; every digit printed with new-line delimiter; etc. Your call. - You are allowed to take the digits in any order you'd like, so they can already be sorted from lowest to highest or vice-versa.
- If no valid time can be created with the given digits (i.e.
2,5,5,5,5,5
), make so clear in any way you'd like. Can returnnull
/false
;"Not possible"
; crash with an error; etc. (You cannot output an invalid time like55:55:52
, or another valid time like00:00:00
.) Please state how it handles inputs for which no valid time can be created. - You are not allowed to output all possible valid times. Only the earliest/latest should be outputted/returned.
24
for hours (i.e.24:00:00
), or60
for minutes/seconds (i.e.00:60:60
) are not valid. The ranges are[00-23]
for hours and[00-59]
for minutes and seconds.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
Input: Earliest output: Latest output:
1,2,3,4,6,8 12:36:48 23:48:16
2,5,5,5,5,5 None possible None possible
0,0,0,1,1,1 00:01:11 11:10:00
1,1,2,2,3,3 11:22:33 23:32:11
9,9,9,9,9,9 None possible None possible
2,3,5,5,9,9 23:59:59 23:59:59
1,2,3,4,5,6 12:34:56 23:56:41
0,0,0,0,0,0 00:00:00 00:00:00
1,5,5,8,8,8 18:58:58 18:58:58
1,5,5,5,8,8 15:58:58 18:58:55
1,1,1,8,8,8 18:18:18 18:18:18
1Isn't
23:48:16
a valid output for the example? – TFeld – 2018-05-09T07:21:34.310should I output only one of the earliest / latest time or both? – tsh – 2018-05-09T07:34:27.687
@tsh Just one. Which one is up to you. The two Python answers thus far output the earliest. – Kevin Cruijssen – 2018-05-09T07:35:10.990
Does a "valid time" not account for any leap seconds? For example, would
06:08:60
be valid, given that there has been a leap second during that minute? – Erik the Outgolfer – 2018-05-09T11:45:02.647@EriktheOutgolfer No,
60
for minutes and seconds is not valid. Ranges are[00-23]
,[00-59]
, and[00-59]
. Will clarify this in the challenge. – Kevin Cruijssen – 2018-05-09T11:52:40.107