13
\$\begingroup\$

Your job is write a program that prints all times (in the format HH:MM, 24 hours) that follow any of the following patterns:

  • Hours equal to minutes, e.g. 22:22,01:01
  • Hours equal to reverse minutes, e.g. 10:01, 01:10, 22:22
  • Sequences, that match H:MM, or HH:MM, always printed with HH:MM. E.g. 00:12, 01:23, 23:45, 03:45, etc (always a single step between digits)

Rules:

  • You may choose any language you like
  • You cannot print repeated times
  • One time per line, following the order of the day
  • The winner will be chosen in February 5.

PS: this is my first question, it might have some inconsistencies. Feel free to edit.

PS2: Here is the expected 44 solutions (already presented by Josh and primo, Thanks!)

00:00
00:12
01:01
01:10
01:23
02:02
02:20
02:34
03:03
03:30
03:45
04:04
04:40
04:56
05:05
05:50
06:06
07:07
08:08
09:09
10:01
10:10
11:11
12:12
12:21
12:34
13:13
13:31
14:14
14:41
15:15
15:51
16:16
17:17
18:18
19:19
20:02
20:20
21:12
21:21
22:22
23:23
23:32
23:45
\$\endgroup\$
18
  • 2
    \$\begingroup\$ I'd expect that 3:45 is written 03:45 in HH:MM format and thus not a sequence. \$\endgroup\$
    – Howard
    Jan 28, 2014 at 18:22
  • \$\begingroup\$ right! I miss that in the examples \$\endgroup\$
    – RSFalcon7
    Jan 28, 2014 at 18:24
  • 3
    \$\begingroup\$ does 13:57 count as a sequence? What about 6:54? On the same note, 6:42 needs to be decided upon, too. \$\endgroup\$ Jan 28, 2014 at 19:37
  • 3
    \$\begingroup\$ Do times with only a single digit that's not a leading zero such as 00:01 count as a sequence? \$\endgroup\$
    – Josh
    Jan 28, 2014 at 21:17
  • 2
    \$\begingroup\$ @RSFalcon7 So only some leading zeroes are ignored? Which ones? 23:45, 02:34, 00:23, 00:02 would seem to be similar cases. \$\endgroup\$ Jan 28, 2014 at 21:39

10 Answers 10

4
\$\begingroup\$

Golfscript (82 72)

Still very much a beginner, but there was no GS answer, so... :)

24,{'0'\+-2>..+\.-1%+}%5,{'0'7,{+}/>4<.(;0\+}%|{2=54<},$);{2/~':'\++}%n*
\$\endgroup\$
2
  • \$\begingroup\$ PHP isn't going to be able to beat that. \$\endgroup\$
    – primo
    Jan 30, 2014 at 16:15
  • \$\begingroup\$ And C definitely won't be able to beat that. \$\endgroup\$
    – Josh
    Jan 30, 2014 at 17:20
4
\$\begingroup\$

PHP - 93 bytes

<?for(;24>$h;)@ereg(+$h=&date(i,$i).$m=date(s,$i++),"0123456$h$h".strrev($h))&&print"$h:$m
";

This will find patterns like 02:34, but will not find patterns like 00:23 or 00:02. If i understand the OP's clarifications in the comments, this is correct.

Prints a total of 44 results:

00:00
00:12
01:01
01:10
01:23
02:02
02:20
02:34
03:03
03:30
03:45
04:04
04:40
04:56
05:05
05:50
06:06
07:07
08:08
09:09
10:01
10:10
11:11
12:12
12:21
12:34
13:13
13:31
14:14
14:41
15:15
15:51
16:16
17:17
18:18
19:19
20:02
20:20
21:12
21:21
22:22
23:23
23:32
23:45
\$\endgroup\$
4
\$\begingroup\$

C, 118 (initially 136)

h,m;main(){h<24&&main(m>57?m=0,h++:h==m|h==m%10*10+m/10|m-12==h*11|h==m-22&!(~-m++%11)&&printf("%02d:%02d\n",h,m-1));}

An iterative version with 119 characters:

h;main(m){for(;h<24;h++)for(m=0;m<57;m++)h==m|h==m%10*10+m/10|m-12==h*11|h==m-22&!(~-m%11)&&printf("%02d:%02d\n",h,m);}

A big thanks to @squeamish ossifrage !

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Should be good now. \$\endgroup\$
    – Josh
    Jan 28, 2014 at 18:47
  • 2
    \$\begingroup\$ My solution outputs those (such as 02:34). The rule m-12==h*11 catches them. \$\endgroup\$
    – Josh
    Jan 28, 2014 at 20:20
  • 2
    \$\begingroup\$ You could replace 100*h+m==1234||100*h+m==2345 with ((m-h)==22)&&!((m-1)%11) (maybe even with fewer brackets) \$\endgroup\$
    – r3mainer
    Jan 28, 2014 at 20:55
  • 1
    \$\begingroup\$ Jackpot! Back down to 128 characters. Thanks! \$\endgroup\$
    – Josh
    Jan 28, 2014 at 21:04
  • 1
    \$\begingroup\$ My understanding was to not include sequences that only have a single digit such as 00:01. I will post on the original question for clarification. \$\endgroup\$
    – Josh
    Jan 28, 2014 at 21:16
2
\$\begingroup\$

Python (178)

s=sorted
for i in[x for x in['%04d'%i for i in range(2400)if i/10%10<6]if s(x[:2])==s(x[2:])or len({i-ord(y)for i,y in enumerate(x.lstrip('0'))})==1]:print'%2s:%2s'%(i[:2],i[2:])

Stripping all leading zeroes before sequences gives 57 results in total.

\$\endgroup\$
0
2
\$\begingroup\$

APL (90)

F←{,'ZI2'⎕FMT⍵}⋄↑⊃¨{(F⍺),':',F⍵}/¨Z/⍨{(⍺=⍵)∨(≡/0 1⌽∘F¨⍺⍵)∨∧/¯1=2-/⍎¨(⍕⍺),F⍵}/¨Z←,1-⍨⍳24 60
\$\endgroup\$
1
  • \$\begingroup\$ That's it. I'm off to make a dialect of perl where every keyword and most functions are a single character. +1, btw ;) \$\endgroup\$
    – primo
    Feb 2, 2014 at 7:31
1
\$\begingroup\$

Javascript - 171 chars (5/2/14) !

for(h='00';+h<24;h=(++h<10?'0':'')+h)for(m='00';+m<60;m=(++m<10?'0':'')+m)if(h[1]+h[0]==m||h==m||+m[1]-m[0]==1&&+m[0]-h[1]==1&&(!+h[0]||+h[1]-h[0]==1))console.log(h+':'+m)

I seriously am getting the hang of golfing. If I look back from now, I have cut down almost 30 chars ! JSBin.

Ungolfed (and commented) :

// Note: +'string' is same as 'parseInt(string, 10)'
// Also, this code is not the shortest one, I have purposely made this code longer for
// understanding purposes

for(h = '0'; +h < 24; h = +h + 1 + '') //initialize h(our), loop while it's less than 24
{                                      // increase it by 1 and cast back to string
    for(m = '0'; +m < 60; m= +m + 1 + '') // intialize m(inute), loop while < 60
    {                                  // increase it by 1 and cast back to string
        if(h.length < 2) h = 0 + h;    // if it is '9', convert to '09'
        if(m.length < 2) m = 0 + m;    // if it is '9', convert to '09'

        // Tests for printing
        if(h[0] === m[1] && m[0] === h[1] ||
           h === m ||
           +m[1] - +m[0] === 1 && +m[0] - +h[1] === 1 && (+h[0] === 0 || +h[1] -+ h[0] === 1))
        console.log(h + ':' + m);  // print
    } // inner loop end
}     // outer loop end

187 chars (Old) (4/2/14)

for(h='00';+h<24;h=(+h<9?'0':'')+(+h+1))for(m='00';+m<60;m=(+m<9?'0':'')+(+m+1))if(h[0]==m[1]&&m[0]==h[1]||h==m||+m[1]-m[0]==1&&+m[0]-h[1]==1&&(!+h[0]||+h[1]-h[0]==1))console.log(h+':'+m)

Little bit of experimenting, and lot improvement (9 chars) :) JSBin

196 chars (Old) (3/2/14)

for(h='00';+h<24;h=(+h<9?'0':'')+(+h+1))for(m='00';+m<60;m=(+m<9?'0':'')+(+m+1))if(h[0]==m[1]&&m[0]==h[1]||h==m||+m[1]-m[0]==1&&+m[0]-h[1]==1&&(!+h[0]||+h[1]-h[0]==1))console.log(h+':'+m)

Sat down with a fresh mind and improved it a lot, a one liner :) JSBin.

208 chars (Old) (2/2/14)

for(h='0';+h<24;h=+h+1+''){for(m='0';+m<60;m=+m+1+''){if(h.length<2)h=0+h
if(m.length<2)m=0+m
if(h[0]==m[1]&&m[0]==h[1]||h==m||+m[1]-+m[0]==1&&+m[0]-+h[1]==1&&(+h[0]==0||+h[1]-+h[0]==1))console.log(h+':'+m)}}

Gives exactly the 44 required times (each in new line)

Will keep improving my code.

I would highly appreciate any feedback. Thank you.

\$\endgroup\$
1
  • \$\begingroup\$ Updated Recently !! \$\endgroup\$ Feb 3, 2014 at 12:53
1
\$\begingroup\$

Python 3, 248 characters

Guess I'm a little late and not very good as well, but finally, got my first codegolf thing to share:

I decided to only support real sequences like 01:23 and 23:45, not 00:12. Anyway, I bet theres plenty to do better, so please go ahead and share a comment with me.

import itertools as t
s=sorted
r=range
i=int
d='%02d'
e=d+':'+d
print([e%(i(a[0]),i(a[1])) for a in t.product([d% x for x in r(0,24)], [d% x for x in r(0,60)]) if s(a[0])==s(a[1]) or list(a[0]+a[1])==[str(x) for x in r(i(a[0][0]),i(a[1][-1])+1)]])

Got the very descriptive version along with it on my pastebin

\$\endgroup\$
0
\$\begingroup\$

Delphi

Still working on it but this is what i have so far.
It works, but im sure its possible to get it shorter.
Edit: Prints 48 times.

Without indent 422 characters

uses System.SysUtils,DateUtils;var t:TTime;a,b,c,d:integer;s:string;begin t:=StrToTime('00:01');while t<StrToTime('23:59')do begin s:=FormatDateTime('hhnn',t);a:=StrToInt(s[1]);b:=StrToInt(s[2]);c:=StrToInt(s[3]);d:=StrToInt(s[4]);if((a+1=b)and(b+1=c)and(c+1=d))or((a=0)and(b+1=c)and(c+1=d))or((a=0)and(b=0)and(c+1=d))or((a=d)and(b=c))or((a=c)and(b=d))then WriteLn(FormatDateTime('hh:nn',t));t:=IncMinute(t)end;ReadLn;end.

With indent 557characters

uses
  System.SysUtils, DateUtils;

var
  t:TTime;
  a,b,c,d:integer;
  s:string;
begin
  t:=StrToTime('00:01');
  while t<StrToTime('23:59')do
  begin
    s:=FormatDateTime('hhnn',t);
    a:=StrToInt(s[1]);
    b:=StrToInt(s[2]);
    c:=StrToInt(s[3]);
    d:=StrToInt(s[4]);
    if((a+1=b) and (b+1=c) and (c+1=d)) or
      ((a=0) and (b+1=c) and (c+1=d)) or
      ((a=0) and (b=0) and (c+1=d)) or
      ((a=d) and (b=c)) or ((a=c) and (b=d)) then
      WriteLn(FormatDateTime('hh:nn',t));
    t:=IncMinute(t)
  end;
  ReadLn;
end.
\$\endgroup\$
0
\$\begingroup\$

q (116)

distinct{t:string 00:00+x;$[(t[0 1]in(t[3 4];t[4 3]))|all 1=1_deltas{"I"$x}each t[0 1 3 4];t;"00:00"]}each til 1440

Increments 00:00 to 23:59, casts to string, then check (test 1 or 2) or 3

Output:

("00:00";"01:01";"01:10";"01:23";"02:02";"02:20";"03:03";"03:30";"04:04";"04:40";"05:05";"05:50";"06:06";"07:07";"08:08";"09:09";"10:01";"10:10";"11:11";"12:12";"12:21";"12:34";"13:13";"13:31";"14:14";"14:41";"15:15";"15:51";"16:16";"17:17";"18:18";"19:19";"20:02";"20:20";"21:12";"21:21";"22:22";"23:23";"23:32";"23:45")
\$\endgroup\$
0
\$\begingroup\$

PHP - 56 39 31 bytes

<? http_redirect("goo.gl/W2M5mo")?>

It doesn't bend the rules in any way. You need the pecl_http module installed and short_open_tag set to "1" in php.ini.

\$\endgroup\$
2
  • \$\begingroup\$ +1, if nothing else for sneakiness, although Octave beats it at 38 characters disp(urlread('http://bit.ly/1fzhb3C')) :) \$\endgroup\$ Feb 2, 2014 at 19:59
  • \$\begingroup\$ ...although you should probably consider the custom designed static HTML you're loading a part of the source code, otherwise a static file would do (and equally not be counted...?) \$\endgroup\$ Feb 2, 2014 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.