63
\$\begingroup\$

Every digital clock contains a small creature that has to advance the time every minute [citation needed]. Due to the popularty of digital clocks and the popularity of catching them in the wild, they are nearly extinct in nature which is why in this challenge we try to automate this task:

Given your string of a given time in the 24h format, the task is to advance this time by one minute.

Details

  • Both the input and output must be strings.
  • The time is given as a five character string.
  • Both the hour and the minutes are given as a two digit number: if the number is below 10, there will be a leading zero.
  • The two numbers are separated by a colon.

Examples

Input    Output
00:00    00:01
00:02    00:03
05:55    05:56
09:59    10:00
12:49    12:50
20:05    20:06
23:59    00:00
\$\endgroup\$
3
  • 21
    \$\begingroup\$ Congratulations on finding a challenge in which Excel beats Jelly! \$\endgroup\$
    – ojdo
    Oct 6, 2021 at 13:58
  • 2
    \$\begingroup\$ [citation needed] 🤣 \$\endgroup\$ Oct 7, 2021 at 23:53
  • 4
    \$\begingroup\$ Ref: Pratchett. In Discworld, imps usually do that sort of thing. “Good Morning, Insert Name Here! I am the Dis-Organizer Mark Five, the Gooseberry™. How may I—” \$\endgroup\$
    – roblogic
    Oct 8, 2021 at 3:35

49 Answers 49

1
2
1
\$\begingroup\$

Red, 85 bytes

func[s][p: func[t][pad/left/with t 2 #"0"]s:(load s)+ 0:1 rejoin[p s/1 % 24":"p s/2]]

Try it online!

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

05AB1E, 26 bytes

':¡`©59%(d+24%®>60%‚т‰J':ý

Try it online!

I am sorry.

-1 byte fix thanks to Kevin Cruijsen!!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You're missing a trailing ':ý, since the output must be a string in a similar format as well. You can remove the ï for -1; golf the D, .À+, and s to ©, +, and ® for -2; and the т+€¦ to т‰J for another -1. (PS: We posted our 05AB1E answers almost simultaneously.) \$\endgroup\$ Oct 5, 2021 at 9:16
  • \$\begingroup\$ @KevinCruijsen thanks for the 0 byte fix!!! \$\endgroup\$
    – Wasif
    Oct 5, 2021 at 9:20
  • \$\begingroup\$ -1 actually. ;) You forgot to remove the ï. Integers/strings are interchangeable in 05AB1E, so explicit casting is almost never necessary (unless you're sorting, or unless there is a bug). \$\endgroup\$ Oct 5, 2021 at 9:33
  • \$\begingroup\$ @KevinCruijssen oh yes! Coercion is nice \$\endgroup\$
    – Wasif
    Oct 5, 2021 at 9:59
1
\$\begingroup\$

Vyxal, 25 bytes

24ʁ60ʁẊƛƛS2↳ð0V;\:j;:?ḟ›i

Try it Online!

What a mess. Handling 0-padding, and some slightly cursed vectorising, was a pain.

24ʁ                       # 0...23
   60ʁ                    # 0...59
      Ẋ                   # Cartesian product
       ƛ           ;      # Map...
        ƛ      ;          # Map...
         S                # Stringify
          2↳              # pad left to length 2 (with spaces, we really need zfill)
            ð0V           # Replace spaces with zeroes
                \:j       # Join by colons
                     ?ḟ   # Find input in this
                       ›  # Increment
                    :   i # Index into this
\$\endgroup\$
1
  • \$\begingroup\$ I have an idea for the 0 padding, add each by 100 then cut the head. You can implement in vyxal \$\endgroup\$
    – Wasif
    Oct 6, 2021 at 17:16
1
\$\begingroup\$

Twue, 123 bytes

a::>*
:59$::=$:00
23$:::=00:
0$::=1
1$::=2
2$::=3
3$::=4
4$::=5
5$::=6
6$::=7
7$::=8
8$::=9
9$::=$0
^_::=!_^
!_::~_
::=
^a$

Try it on the website!

Works by taking input through a, having two replacement rules governing roll-over (:59 -> :00 and add 1, 23$: -> 00:). Then, we have a rule for incrementing, rolling over only when we increment a 9. After that, we output everything.

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

Haskell, 130 bytes

import Text.Printf
f(a:b:c:d)|[m,h]<-map read[d,a:[b]]::[Int]=[mod(m+1)60,mod(h+m`div`59)24]
g s|[m,h]<-f s=printf"%0.2d:%0.2d"h m

Try it online!

Haskell is missing, hope to see a better one because I feel like mine is a bit messy

  • f reads the input string and increments the values properly
  • g prints/converts back values using C-like formatting of Text.Printf
\$\endgroup\$
1
\$\begingroup\$

Slim Sharp 46 Bytes

P(T.Parse(t).AddMinutes(1).ToString("HH:mm"));

Try it Online!

How it Works
P(                               // Write the contents to the console.
  T.Parse(t)                     // Parse the input as a date time object.
            .ToString(           // Output the date time object as a string.
                      "HH:mm"    // Format the output in 24 hour time with hours and minutes only.
                             )); // End statement.
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 47 bytes

StringTake[ToString@DatePlus[#,1/1440],-{8,4}]&

Try it online!

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

Python 3.8, 66 bytes

lambda t:f'{(int(t[:2])+((m:=(int(t[3:])+1)%60)<1))%24:02}:{m:02}'

Try it online!

A slightly different approach to @dingledooper's answer (and 2 bytes longer 😕), but the same basic idea.

\$\endgroup\$
1
  • \$\begingroup\$ You can save two bytes on parentheses by using -~ to increment: Try it online! \$\endgroup\$
    – DLosc
    Sep 21 at 16:19
1
\$\begingroup\$

Julia 1.0, 43 42 bytes

using Dates
~t="$(Time(t)+Minute(1))"[1:5]

Try it online!

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

PowerShell, 45 bytes

(Get-date $x).AddMinutes(1).ToString("HH:mm")

Try it online

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Golf! Nice answer. \$\endgroup\$ Oct 14, 2021 at 19:29
1
\$\begingroup\$

Visual Basic, 49 bytes (format command)

Dim ins$, out$

ins = "20:21"
out = Format$(DateAdd("n", 1, TimeValue(ins)), "hh:mm")

Uses internal time functions.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code golf, and nice first answer! Here, we usually require that answers be golfed, which you can do by removing whitespace and shortening variable names - also check out our Tips for golfing in Visual Basic page. You could add a TIO link so others can test your code online. \$\endgroup\$
    – emanresu A
    Oct 14, 2021 at 20:40
1
\$\begingroup\$

Perl 5 -F:, 67 64 bytes

63 60 bytes for code; +4 bytes for -F switch
-3 bytes thanks to erlik

@F=++$F[0],0if++$F[1]>59;$F[0]*="@F"<24;printf"%02d:%02d",@F

Try it online!

Ungolfed version:

if (++$F[1] > 59) {
    @F = (++$F[0], 0);
}
$F[0] *= "@F" < 24;
printf("%02d:%02d", @F);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can make it 3 chars shorter by replacing the second statement with $F[0]*="@F"<24 \$\endgroup\$
    – Erlik
    Oct 14, 2021 at 19:37
1
\$\begingroup\$

Zsh builtins (no date utils), 66 bytes

Reinventing the wheel... Try it Online!

m=$[(${1:3}+1)%60]
printf %02d:%02d $[(${1:0:2}+(m==0?1:0))%24] $m
\$\endgroup\$
1
\$\begingroup\$

Python 3.8, 86 bytes

Little late on this one, but haven't done code golf in a long time and was bored :)

EDIT: -8 bytes

lambda s:'%02d:%02d'%([x:=int(s[:2]),[x+1,0][x>22]][(y:=int(s[3:])+1)>59],[y,0][y>59])

Try it online!

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

Python 3.8 (pre-release), 88 bytes

def f(s):h,m=int(s[:2]),int(s[-2:]);m=(m+1)%60;m or(h:=(h+1)%24);return f'{h:02}:{m:02}'

Try it online!

Very straightforward solution here. Nowhere near competitive and nothing novel, but I enjoyed this! I learned a new trick along the way:

m or(h:=(h+1)%24) is equivalent to if m<1:h=(h+1)%24 and the same length, but allows the whole function to be collapsed into one line with semicolons, saving four characters. Without that, this would be four lines.

def f(s):
 h,m=int(s[:2]),int(s[-2:]);m=(m+1)%60
 if m<1:h=(h+1)%24
 return f'{h:02}:{m:02}'

Other fun equivalences I learned today:

m=(m+1)%60 is the same length as
m+=1;m%=60

return f'{h:02}:{m:02}' is the same length as
return'%02d:%02d'%(h,m)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Another fun thing: -~m is equivalent to m+1, except it doesn't need the parentheses. Also, s[-2:] can be s[3:]. Try it online! \$\endgroup\$
    – DLosc
    Sep 21 at 5:16
  • \$\begingroup\$ Lovely! Thank you \$\endgroup\$
    – Pandu
    Sep 21 at 14:58
0
\$\begingroup\$

In Go you can do this 153 bytes

package main

import (
    "fmt"
    "time"
    "os"
)

func main() {
    t,_ := time.Parse(`15:04`, os.Args[1])  
    fmt.Print(t.Add(time.Minute).Format(`15:04`))
}

Try it online

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to the site. I'm a little confused as to how you are counting bytes here. The first block is ~174 bytes and the second block is 78 bytes, so I'm not sure what code you are counting. I also don't think either of these take any input? They seem to do the operation on in the first case a fixed string and in the second case it seems to expect the input in a variable named i. You can see a variety of ways you can take input here. \$\endgroup\$
    – Wheat Wizard
    Oct 15, 2021 at 8:19
  • \$\begingroup\$ I was counting just by selecting code, and seeing whatever Sublime gave me. About input output, also wasn't sure how to deliver data. I fixed my answer to take input from cmd line. \$\endgroup\$
    – Slav
    Oct 15, 2021 at 9:08
  • 1
    \$\begingroup\$ Hm. I have no idea what sublime is doing. Maybe it isn't counting whitespace or something? You can use TIO to get an accurate score link. We also have a tips question where you can get tips on golfing in go. I can already see that there are some ways that you could shorten this so try looking at that and playing around with whitespace. \$\endgroup\$
    – Wheat Wizard
    Oct 15, 2021 at 9:15
0
\$\begingroup\$

K, 9 bytes

-3!1+-5!x

Q, 14 bytes:

string 1+"U"$x

Explanation

Q is evaluated from right to left:

         "U"$x          // Cast input x into the minutes types
       1+               // Add one (automatically casted into minutes)
string                  // Cast to string

For this specific input string, the casting part can also be written as

parse x

Resulting in a quite readable 16 bytes:

string 1+parse x

Replacing string and parse with the K internal function equivalents (-3! and -5! respectively) we get:

-3!1+-5!x

q/kdb+ CLI

\$\endgroup\$
1
  • \$\begingroup\$ Minor caveat: input 23:59 will yield 24:00 output, not 00:00. This can be fixed adding mod: -3!1440 mod 1+-5!x (+9 bytes). \$\endgroup\$ Oct 27, 2021 at 0:58
0
\$\begingroup\$

Python 3.8, 118 114 113 112 bytes

New version

i=input();t=[-1]*59+[59];h=[0]*24+[-23];a=int(i[:2]);b=int(i[3:]);print(f'{a+h[a+int(t[b]/30)]:02}:{b-t[b]:02}')

Old version:

i=input();t=(1,0)*59+(-59,1);h=[0]*24+[-23];a=int(i[:2]);b=int(i[3:]);print(f'{a+h[a+t[b*2+1]]:02}:{b+t[b*2]:02}')

Older version:

i=input();t=[(1,0)]*59+[(-59,1)];h=[0]*23+[-24];a=int(i[:2]);b=int(i[3:]);print(f'{a+h[a]+t[b][1]:02}:{b+t[b][0]:02}')

Try it here!

Cleaner version

i=input()
t=[1]*59+[-59]
h=[0]*24+[-23]
a=int(i[:2])
b=int(i[3:])
print(f'{a+h[a+int(t[b]/-30)]:02}:{b+t[b]:02}')

A somewhat dumb but interesting approach I tried, I am pretty sure I can probably shorten this somewhere by 15-20~ Bytes, I will have to check it again later. The method uses two lookup arrays to figure out what to do with the date. Its worse than the other answers, but I thought its interesting enough to leave in after golfing it a bit. I am not really good at this yet haha.

Some thoughts that would be interesting to discuss, I see a few places where I can see an improvement being possible, but not sure how:

  1. In the part (a=int(i[:2]);b=int(i[3:]); this saves a number of bytes since I use a and b alot, but its still a very... ugly solution for the lack of a better word, I looked into a,b=map(int,i.split(":")) which looks neater but unfortunately has the exact same size. I am still looking for a better solution here though.

  2. I really hate how the lists/tuples are initialized, must be a better way..

Edit: I legitimately have no idea how I came up with this madness, but some weird maths helped me remove an extra byte, somehow haha...

I am certain I can push this further later..

Dang, just as I posted this I thought of a simple change with the signs that did save me another byte.

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

Knight (v2.0-alpha), 70 bytes

;=t+1+*+0=iP60Gi 3 2O++G=x+'0'%/t 60 24-Lx 2 2':'(G=x+'0'%t 60-Lx 2 2)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ you don't need the parens (btw you can use the minify button to remove al the excess whitespace and parens and stuff) \$\endgroup\$
    – Steffan
    Sep 21 at 18:44
  • \$\begingroup\$ i was gonna suggest +0 => E but i guess that's not part of the spec anymore \$\endgroup\$
    – Steffan
    Sep 21 at 18:45
1
2

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.