# Find all times that follow a pattern

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
• I'd expect that 3:45 is written 03:45 in HH:MM format and thus not a sequence. – Howard Jan 28 '14 at 18:22
• right! I miss that in the examples – RSFalcon7 Jan 28 '14 at 18:24
• does 13:57 count as a sequence? What about 6:54? On the same note, 6:42 needs to be decided upon, too. – John Dvorak Jan 28 '14 at 19:37
• Do times with only a single digit that's not a leading zero such as 00:01 count as a sequence? – Josh Jan 28 '14 at 21:17
• @RSFalcon7 So only some leading zeroes are ignored? Which ones? 23:45, 02:34, 00:23, 00:02 would seem to be similar cases. – Joachim Isaksson Jan 28 '14 at 21:39

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*  • PHP isn't going to be able to beat that. – primo Jan 30 '14 at 16:15 • And C definitely won't be able to beat that. – Josh Jan 30 '14 at 17:20 ## 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  ## 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 ! • Should be good now. – Josh Jan 28 '14 at 18:47 • My solution outputs those (such as 02:34). The rule m-12==h*11 catches them. – Josh Jan 28 '14 at 20:20 • You could replace 100*h+m==1234||100*h+m==2345 with ((m-h)==22)&&!((m-1)%11) (maybe even with fewer brackets) – r3mainer Jan 28 '14 at 20:55 • Jackpot! Back down to 128 characters. Thanks! – Josh Jan 28 '14 at 21:04 • 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. – Josh Jan 28 '14 at 21:16 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. ## APL (90) F←{,'ZI2'⎕FMT⍵}⋄↑⊃¨{(F⍺),':',F⍵}/¨Z/⍨{(⍺=⍵)∨(≡/0 1⌽∘F¨⍺⍵)∨∧/¯1=2-/⍎¨(⍕⍺),F⍵}/¨Z←,1-⍨⍳24 60  • That's it. I'm off to make a dialect of perl where every keyword and most functions are a single character. +1, btw ;) – primo Feb 2 '14 at 7:31 # 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+h==m||h==m||+m-m==1&&+m-h==1&&(!+h||+h-h==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 === m && m === h || h === m || +m - +m === 1 && +m - +h === 1 && (+h === 0 || +h -+ h === 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==m&&m==h||h==m||+m-m==1&&+m-h==1&&(!+h||+h-h==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==m&&m==h||h==m||+m-m==1&&+m-h==1&&(!+h||+h-h==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==m&&m==h||h==m||+m-+m==1&&+m-+h==1&&(+h==0||+h-+h==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. • Updated Recently !! – Gaurang Tandon Feb 3 '14 at 12:53 # 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),i(a)) for a in t.product([d% x for x in r(0,24)], [d% x for x in r(0,60)]) if s(a)==s(a) or list(a+a)==[str(x) for x in r(i(a),i(a[-1])+1)]])  Got the very descriptive version along with it on my pastebin # 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);b:=StrToInt(s);c:=StrToInt(s);d:=StrToInt(s);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); b:=StrToInt(s); c:=StrToInt(s); d:=StrToInt(s); 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.  # 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")


# PHP - 5639 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.

• +1, if nothing else for sneakiness, although Octave beats it at 38 characters disp(urlread('http://bit.ly/1fzhb3C')) :) – Joachim Isaksson Feb 2 '14 at 19:59
• ...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...?) – Joachim Isaksson Feb 2 '14 at 23:19

## TI-BASIC

DelVar AWhile A<24:A+1→A:Disp A,":",A:End