9
\$\begingroup\$

I happened to glance at my watch today at exactly 11:11:11 (and today is 1/11; too bad it's not 2011), and that got me thinking: I know! I should make a code golf question out of this! I'm a dork.

Anyway, your challenge is to take an hour, minute, and second as input, and output the next "interesting" time. Here, I will define interesting as these steps:

  1. Concatenate the hour, minute, and second. (For example, at 4:14:14, this would be 41414.)
  2. Check for consecutive groups of one, two, or three that span the length of the entire string. For example, I could find [41][41][4] in the example time (if the group cannot reach through the string, just cut it off like I did in this example). Another example: in the time in my first example at the beginning of the question, it would be [1][1][1][1][1][1], [11][11][11], or [111][111].
  3. Is there a consecutive group that goes all the way through the string? If so, the time is "interesting!" Otherwise, it's not.

The input can be in any reasonable format, but it must not be hardcoded. The output can also be in any reasonable format, and it need not be in the same format as the input.

If you use network access for some reason, all bytes downloaded from the network count to your score.

This is ; shortest code in bytes wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ It's January, not November :P \$\endgroup\$ – Volatility Jan 12 '14 at 1:19
  • \$\begingroup\$ @Volatility Whoops, typo :-P fixed \$\endgroup\$ – Doorknob Jan 12 '14 at 1:32
  • 1
    \$\begingroup\$ I like the network access restriction. \$\endgroup\$ – Kyle Kanos Jan 12 '14 at 1:35
  • 1
    \$\begingroup\$ Should code only consider times on a 12 hour clock? For example, on a 24 hour clock 14:14:14 would be an interesting time but not so interesting on a 12 hour clock (2:14:14) \$\endgroup\$ – Kevin Anderson Jan 12 '14 at 17:25
2
\$\begingroup\$

J, 113 99 90

Probably still quite golfable.

f=:t#:[:(>:^:([:(3&g+:2&g=.[:*/]=(]$)${.)@(":@{.,3}.7":100#.1,])t#:])^:_)1+(t=.24,2$60)#.]

Takes a vector (h m s) as input, and returns a vector in the same format as output.

Examples:

   f 0 0 0
0 1 0
   f 4 14 14
4 14 41
   f 23 59 59
0 0 0
   f 3 14 15
3 14 31
\$\endgroup\$
1
\$\begingroup\$

Haskell - 227223

That's one way of doing it.

    import Data.List
e _ []=False
e f s=let (h,t)=f s in t`isPrefixOf`h||h`isPrefixOf`t&&e f t
i [a,b,s]=head[t|s<-[s+1..],f<-map splitAt[1..3],let m=b+s`div`60;h=a+m`div`60;t=[h`mod`24,m`mod`60,s`mod`60],e f$concatMap show t]

Examples

λ: i [11,11,11]
[11,21,1]
λ: i [4,14,14]
[4,14,41]
\$\endgroup\$
  • \$\begingroup\$ Could you post a sample run? I'm clueless about Haskell so I have no idea how the input/output works, etc :-P \$\endgroup\$ – Doorknob Jan 12 '14 at 4:19
1
\$\begingroup\$

Mathematica 125

F=Do[#~MatchQ~{#〚-1〛..,_}&&Break@#&@Partition[(i=IntegerDigits)@f[n~i~60,100],m,m,1,a_],
{n,#~(f=FromDigits)~60+1,7^6},{m,3}]&

It returns a pattern of the next interesting time:

F@{11, 11, 11}
F@{4, 14, 14}

{{1, 1, 2}, {1, 1, 2}}

{{4, 1, 4}, {4, 1, a_}}

a_ marks the end of the time.

\$\endgroup\$
1
\$\begingroup\$

Lua

I've got four different solutions, as I wasn't sure about some of the requirements.

Version 1: removal of 0s, command line input as well as os.time() backup (315)

Minimised:

z=arg if z[1] then y={hour=z[1],min=z[2],sec=z[3],day=1,month=1,year=1}end h,g,e,u=os.date,os.time(y),":",tonumber while 1 do g=g+1 b,c,d=u(h("%H",g)),u(h("%M",g)),u(h("%S",g)) a=b..c..d for x=1,#a/2 do p=1 for _ in a:gmatch(a:sub(1,x))do p=p+1 if p>math.ceil(#a/x) then print(b..e..c..e..d)return 1 end end end end

Full version with comments:

z=arg if z[1] then y={hour=z[1],min=z[2],sec=z[3],day=1,month=1,year=1}end --get command line arguments
h,g,e,u=os.date,os.time(y),":",tonumber --set up references, if command line arguments accepted use y else use current time
while 1 do
    g=g+1
    b,c,d=u(h("%H",g)),u(h("%M",g)),u(h("%S",g)) --get HH:MM:SS seperately (which allows removal of the zeroes with tonumber())
    a=b..c..d  --concat
    for x=1,#a/2 do  --check up to half of the string
        p=1
        for _ in a:gmatch(a:sub(1,x))do --for each match
            p=p+1  --count number of matches
            if p>math.ceil(#a/x) then print(b..e..c..e..d)return 1 end --if matches span entire string, cheer (and print in a pretty format)
        end
    end
end

The other versions are very similar, so I will only post the minimised versions:

Version 2: no command line input (239)

h,g,e,u=os.date,os.time(),":",tonumber while 1 do g=g+1 b,c,d=u(h("%H",g)),u(h("%M",g)),u(h("%S",g)) a=b..c..d for x=1,#a/2 do p=1 for _ in a:gmatch(a:sub(1,x))do p=p+1 if p>math.ceil(#a/x) then print(b..e..c..e..d)return 1 end end end end

Version 3: no 0 removal, with command line input (240)

z=arg if z[1] then y={hour=z[1],min=z[2],sec=z[3],day=1,month=1,year=1}end h,g=os.date,os.time(y) while 1 do g=g+1 a=h("%H%M%S",g) for x=1,3 do p=1 for _ in a:gmatch(a:sub(1,x))do p=p+1 if p>6/x then print(h("%T",g))return 1 end end end end

Version 4: none of the fancy stuff (no 0 removal or command line input) (164)

h,g=os.date,os.time() while 1 do g=g+1 a=h("%H%M%S",g) for x=1,3 do p=1 for _ in a:gmatch(a:sub(1,x))do p=p+1 if p>6/x then print(h("%T",g))return 1 end end end end

Usage instructions

In a terminal, run (versions 1 and 3)

lua interesting.lua HOURS MINUTES SECONDS

or just

lua interesting.lua

If you want it to go off the system clock.

Is there a prize for feature completeness? :P

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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