| Bytes | Lang | Time | Link |
|---|---|---|---|
| 064 | Raku Perl 6 rakudo | 241210T092745Z | bb94 |
| 014 | Japt | 180509T080929Z | Shaggy |
| 030 | MATL | 180720T195214Z | Sundar R |
| 389 | Kotlin | 180619T131447Z | JohnWell |
| 055 | Ruby | 180509T073510Z | G B |
| 062 | Retina | 180509T113640Z | Neil |
| 047 | Retina | 180511T150521Z | TwiN |
| 015 | Stax | 180511T061342Z | recursiv |
| 069 | Bash + GNU sed | 180509T143238Z | zeppelin |
| 124 | Red | 180509T092914Z | Galen Iv |
| 073 | Perl 5 with palF | 180509T113714Z | Dom Hast |
| 023 | Japt | 180509T080752Z | Etheryte |
| 086 | Haskell | 180509T162501Z | Angs |
| 037 | Pyth | 180509T165156Z | hakr14 |
| 174 | C gcc | 180509T150356Z | LambdaBe |
| 088 | JavaScript ES6 | 180509T085156Z | Arnauld |
| 043 | Perl 6 | 180509T131045Z | nwellnho |
| 015 | Jelly | 180509T132132Z | Erik the |
| 078 | Python 2 | 180509T105703Z | lynn |
| 015 | 05AB1E | 180509T112333Z | Emigna |
| 063 | Wolfram Language Mathematica | 180509T082101Z | JungHwan |
| 017 | Jelly | 180509T082807Z | Jonathan |
| 088 | Python 2 | 180509T071548Z | TFeld |
Raku (Perl 6) (rakudo), 64 bytes
Takes a list of numbers. Outputs the earliest time as a list of numbers or Nil if none.
{.permutations.sort.grep({"2 4"gt.[^2]&&"6 0"gt.[^2+(2&4)]})[0]}
Japt, 17 15 14 bytes
Takes input as a string of digits and outputs the first valid time; loops infinitely if there's no valid time. Locale/browser dependent on time strings being of the format hh:mm:ss.
Èá6 øU}a@ÐX s8
Warning: The solution works in increments of milliseconds, so, as such, it is
extremelyobscenely slow - the*1000after theXin the linked version instead makes it work in seconds in an effort to speed it up somewhat. An invalid input will still create an infinite loop, though, and may crash your browser.
Explanation
The JavaScript Date object (Ð in Japt) is great for golfing with because of its variable arity. When it's provided with just a single integer argument that integer is interpreted to mean milliseconds since epoch began (1970-01-01T00:00:00.000Z)
Èá6 øU}a@ÐX s8 :Implicit input of string U
È :Left argument taking a time string ("hh:mm:ss") as argument
á6 : Permutations of length 6
øU : Contains U?
} :End function
a :Return the first result of the right function that returns true when passed through the left function
@ :Right function taking an iteration index X>=0 as argument
ÐX : Create Date object as described above
s8 : Convert to locale time string
MATL, 31 30 bytes
Y@3Xy[X1]X*!Y*t[4XX]6*<!AY)1Y)
Input is 6 integers, output is the minimum hour, minutes, and seconds in an array. Crashes for inputs where no such time is possible.
(-1 byte thanks to @Luis Mendo.)
Kotlin, 396 391 389 bytes
No clue how to make this smaller. I'm thinking it is double of what's possible. Produces earliest time. Thanks to Kevin for 7 bytes!
fun p(d:Array<Int>)={val s=Array(6,{0})
val f=Array(6,{1>0})
val t=Array(3,{0})
val o=Array(3,{60})
fun r(i:Int){if(i>5){var l=0>1
var e=!l
for(p in 0..2){t[p]=s[p*2]*10+s[p*2+1]
l=l||(e&&t[p]<o[p])
e=e&&t[p]==o[p]}
if(t[0]<24&&t[1]<60&&t[2]<60&&l)for(p in 0..2)o[p]=t[p]}
else
for(p in 0..5)if(f[p]){f[p]=0>1
s[i]=d[p]
r(i+1)
f[p]=1>0}}
r(0)
if(o[0]>23)0
else "${o[0]}:${o[1]}:${o[2]}"}()
Ruby, 68 67 62 56 55 bytes
->*b{b.permutation.find{|a,b,c,d,e|c<6&&e<6&&a*9+b<22}}
Input: Sorted array of digits (as integers).
Output: Array of digits or nil if no solution found
Retina, 77 74 69 65 62 bytes
$
:
6+Lv$`(.)(.*):
$%`$2:$1$%'
O`
0L`([01].|2[0-3])([0-5].){2}
Try it online! Outputs the earliest time, or the empty string if no time can be found. Edit: Saved 5 8 bytes thanks to @TwiNight. Explanation:
$
:
6+Lv$`(.)(.*):
$%`$2:$1$%'
Generate all the permutations. The : works its way though the string as the permutations are generated, ending up at the start.
O`
Sort the times into order.
0L`([01].|2[0-3])([0-5].){2}
Output the first valid time.
Retina, 58 47 bytes
+,V^2`[0-5][6-9]{2}
G`([01].|2[0-3])([0-5].){2}
Input is 6 digits in sorted order. Output is 6 digits representing earliest valid time, or empty string if no valid time exists.
EDIT: I was an idiot, -9 bytes
Explanation
Algorithm
For brevity, let's define a low digit as 0-5, and a high digit as 6-9.
First, rearrange the digits so that "low-ness" or "high-ness" of each position is correct. The correct arrangement, for each number of high digits in the input:
# of highs arrangment
0 LLLLLL
1 LLLLLH
2 LLLHLH
3 LHLHLH
4+ Not possible
Since the any rearrangement would fail the final check in the input has 4+ high digits, we can ignore that case completely.
Then, sort the lows and the highs individually. Combine with the rearrangement, this gives the lowest value that satisfies the minute and second constraints. So this gives the earliest valid time, if one exists.
Finally, check if that we have valid time. If not, discard the string.
Program
+,V^2`[0-5][6-9]{2}
Matches LHH and swaps the first two digits in that (becomes HLH), and repeat that until no more LHH exists. This gives the correct arrangement.
Actually, I lied. No sorting is needed because 1) swapping only happens between adjacent digits and only between a low and a high; and 2) the input is sorted. So the lows and the highs individually are already in sorted order.
G`([01].|2[0-3])[0-5].[0-5].
Only keeps the string if it is a valid time
Stax, 15 bytes
╝a╣=→aá≈#8(⌂≈58
It takes a string of sorted digits for input. It returns the first permutation that satisfies a few criteria.
- lexicographically less than "24"
- all three pairs of characters are lexicographically less than "6"
Bash + GNU sed, 83, 72, 69 bytes
- Accepts input as 6 separate arguments;
- Returns the earliest time (if found);
- Returns nothing (empty output) if no valid combination exist.
seq 0 86399|sed "s/^/date +%T -ud@/e;h;`printf s/%d//\; $@`/\w/d;x;q"
How it works
Pre-generate all the possible time strings, for the timestamps in range 0 to 86399, using GNU-sed e(xecute) command + date.
%seq 0 86399|sed "s/^/date +%T -ud@/e;h;"
00:00:00
00:00:01
...
23:59:59
Generate sed script with 6 sequential substitution commands,
for an each input digit.
%echo sed `printf s/%d//\; $@`
sed s/1//;s/2//;s/3//;s/4//;s/6//;s/8//;
Then, apply substitutions, remove any input lines which have at least one digit left, print the first matching line (original time string is extracted from the hold space with x).
%echo 23:45:12|sed 's/1//;s/2//;s/3//;s/4//;s/6//;s/8//;'
:5:2 //non-matching, delete
%echo 12:36:48|sed 's/1//;s/2//;s/3//;s/4//;s/6//;s/8//;'
:: //matching, print and stop
Test
%./timecomb 1 2 3 4 6 8
12:36:48
%./timecomb 2 5 5 5 5 5
%./timecomb 0 0 0 1 1 1
00:01:11
%./timecomb 1 1 2 2 3 3
11:22:33
%./timecomb 9 9 9 9 9 9
%./timecomb 2 3 5 5 9 9
23:59:59
%./timecomb 1 2 3 4 5 6
12:34:56
%./timecomb 0 0 0 0 0 0
00:00:00
%./timecomb 1 5 5 8 8 8
18:58:58
%./timecomb 1 5 5 5 8 8
15:58:58
%./timecomb 1 1 1 8 8 8
18:18:18
Red, 157 124 bytes
Thanks to Kevin Cruijssen for reminding me to read the descritions more carefully!
func[s][a: 0:0:0 loop 86400[b: to-string a a: a + 1 if b/1 =#"0"[insert b"0"]if s = sort replace/all copy b":"""[return b]]]
Takes a sorted string as input. Returns none if it's not possible to make time.
Explanation:
f: func[s][ ; the argument is a sorted string of digits
a: 0:0:0 ; time object, set to 00:00:00 h
loop 86400 [ ; loop through all seconds in 24 h
b: to-string a ; convert the time to string
a: a + 1 ; add 1 second to the current time
if b/1 = #"0" [ ; prepend "0" if necessary
insert b "0" ; (Red omits the leading 0)
]
if s = sort replace/all copy b ":" ""[ ; compare the input with the sorted time
return b ; return it if they are equal
]
]
]
Perl 5 with -palF, 73 bytes
$"=",";($_)=grep@F~~[sort/./g]&/([01]\d|2[0-3])([0-5]\d){2}/,glob"{@F}"x6
Outputs like HHmmss and outputs a blank line for invalid entries.
Every answer I've made recently has used globing for permutations... Weird!
Japt, 39 23 bytes
Pretty sure there's a shorter way to do this, but I wanted to try using Date objects in Japt.
á ®¬ò q':Ãf@T<ÐXiSiKÅ
Ì
á // Get all permutations of the input array.
®¬ò q':à // [1,2,3,4,5,6] -> "12:34:56"
f@ // Filter the results, keeping those that
T< // are valid dates
ÐXiSiKÅ // when made into a date object.
Ì // Return the last remaining item, if any.
Takes input as a sorted array of numbers, returns the latest valid time or empty output if none exists.
Lost 10 pounds bytes thanks to Shaggy.
Haskell, 114 96 86 bytes
import Data.List
f l=minimum[x|x@[a,b,c,d,e,f]<-permutations l,a:[b]<"24",c<'6',e<'6']
Now with less strict output. Takes input as a string of digits and compares permutations against limits with list comparison. With minutes and seconds only the first digit is checked. Crashes and burns if no permutation is a valid time.
Pyth, 37 bytes
j\:hf&&<shT24<s@T1 60<seT60mcs`Md2S.p
Explanation:
j\:hf&&<shT24<s@T1 60<seT60mcs`Md2S.pQ # Code with implicit variables
h # The first element of
.pQ # The list of all permutations of the input list
S # Sorted
mcs`Md2 # Mapped to three two digit long strings
f # Filtered on whether
<shT24 # The first number is less than 24
& <s@T1 60 # AND the second number is less than 60
& <seT60 # AND the third number is less than 60
j\: # Joined by a colon
C (gcc), 186 174 bytes
D[7]={0,1,10,100,1e3,1e4,1e5};G(O,L,F,T,I,M,E){if(!F)O=L<24e4&L%10000<6e3&L%100<60?L:1e9;else{for(T=1e9,I=0;I++<F;M=G(O/10,L*10+E,F-1),T=T>M?M:T,O=(O/10)+E*D[F])E=O%10;O=T;}}
-12 bytes thanks to Kevin Cruijssen
Probably not optimal, but it works. Oddly enough for some reason with 7 arguments the gcc implementation on TIO requires that you actually supply them or it segfaults. On my machine that is unnecessary however.
Format: G(X,0,6) -> Y where X is the 6-digit number whose digits are to be used and Y is the 6 digit number which when taken as a time (by inserting : appropriately) is minimal.
JavaScript (ES6), 93 89 88 bytes
Expects an array of 6 digits, sorted from lowest to highest. Returns either the 6-digit string of the first valid time, or false if no solution exists.
f=(a,t='')=>t<24e4&/..([0-5].){2}/.test(t)?t:a.some((v,i)=>s=f(a.filter(_=>i--),t+v))&&s
Commented
We recursively try all permutations of the input until we find one that passes a hybrid test using both arithmetic and a regular expression.
f = ( // f = recursive function taking
a, t = '' // a[] = input array and t = current time
) => //
t < 24e4 & // if t is less than 240000
/..([0-5].){2}/.test(t) ? // and it matches "hhMmSs" with M and S in [0-5]:
t // return t
: // else:
a.some((v, i) => // for each digit v at position i in a[]:
s = f( // save in s the result of a recursive call with:
a.filter(_ => i--), // a copy of a[] with the current digit removed
t + v // the current digit appended to t
) // end of recursive call
) && s // end of some(); if truthy, return s
Perl 6, 43 bytes
*.permutations.first:{24e4>.join&&6>.[2&4]}
Expects a sorted input array. Returns Nil for invalid input.
Jelly, 15 bytes
Œ!s€2Ḍf«¥“ç;;‘Ṃ
Posted after a request. The approach is the same as the one of the other answer, however this answer was developed independently.
Python 2, 78 bytes
lambda s:min(x for x in range(62**3)if x%100<60>x/100%100<s==sorted('%06d'%x))
Arnauld saved a byte. Thanks!
Expects a list like ['1','2','3','4','6','8'] in sorted order:
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.
Outputs an integer like 123648 for 12:36:48. I hope that's acceptable.
05AB1E, 20 15 bytes
Input as sorted string.
Output is the smallest time as a string.
In case of no solution, an empty list is the output.
œʒ2ô•3Èñ•2ô‹P}н
Wolfram Language (Mathematica), 63 bytes
FirstCase[Permutations@#,{a:0|1|2,b_,c_,_,d_,_}/;a*b-4<6>d>=c]&
Takes a sorted list of digits as input. Returns Missing[NotFound] for invalid inputs.
Explanation
Permutations@#
Find all of the permutations of the input. Since the input is sorted, it is guaranteed that all valid times are in increasing order.
FirstCase[ ... ]
Find the first list that matches...
{a:0|1|2,b_,c_,_,d_,_}
The first element, labeled a, is 0, 1, or 2, and label the second, third, and fifth elements b, c, and d respectively...
... /;a*b-4<6>d>=c
... such that a*b is less than 10, and d and c are less than 6, with d >= c.
The trick is that for all numbers 00 to 24, the product of the two digits is at most 9, and the possible invalid numbers 25 to 29 (since we force the first digit to be 0, 1, or 2) have the product of least 10.
Jelly, 17 bytes
I'm almost certain this is not the shortest approach... will look at this again later :)
Œ!s2Ḍ<ẠʋÐṀ“ð<<‘ṢḢ
Python 2, 131 115 112 109 105 88 bytes
lambda s:min(d for d in permutations(s)if(2,4)>d[:2]>d[4]<6>d[2])
from itertools import*
I/O are lists of integers
Throws an error if no times are possible
Alternative:
Python 2, 88 bytes
lambda s:max(d*((2,4)>d[:2]>d[4]<6>d[2])for d in permutations(s))
from itertools import*
Returns the latest time
Returns an empty tuple for invalid times
Saved
- -21 bytes, thanks to ovs