67
\$\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
  • 24
    \$\begingroup\$ Congratulations on finding a challenge in which Excel beats Jelly! \$\endgroup\$
    – ojdo
    Commented Oct 6, 2021 at 13:58
  • 2
    \$\begingroup\$ [citation needed] 🤣 \$\endgroup\$
    – Taco
    Commented 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
    Commented Oct 8, 2021 at 3:35

52 Answers 52

1
2
2
\$\begingroup\$

C (gcc), 101 bytes

I was hoping this would be shorter, but I am posting it despite the length because I think it's an interesting way to compute the answer.

This function modifies the input string so the output is the input. I'm not too familiar with the C golfing rules on this but since it's so long I'm neglecting that issue.

This function treats the input as individual characters, where each column has a 'max' value (the first 5 characters in u) and a 'reset' value (last 5 characters in u). First the last character of the input string is incremented, then from right to left each character is compared to the max. If any character is greater than its max, it is set to the 'reset' value of that column and the column to the left is incremented.

As described so far, this function will reset after 29:59. To make it reset after 23:59 I used short *a to check the first two characters at once to see if they equal "24" and if they do, set them to "00".

Also note that I had to modify the test code in order to copy input strings into writable memory.

Improvements are welcome, I think I'm out of ideas on this one.

Thanks to att for -22 bytes, a bugfix, and some awesome golfing!

c;f(char*x){for(x[c=4]++;~c;)x[--c]+=x[c]>"29:59"[c]&&(x[c]=c-2?48:58);*x<49&&x[1]==52?*x=x[1]=48:0;}

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 114 bytes and a bugfix (e.g. "05">"24" is true) \$\endgroup\$
    – att
    Commented Oct 16, 2021 at 5:19
  • 1
    \$\begingroup\$ 103 bytes with a couple alternatives, but getting rid of short* yields 101... \$\endgroup\$
    – att
    Commented Oct 16, 2021 at 5:50
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\$ Commented Oct 5, 2021 at 9:16
  • \$\begingroup\$ @KevinCruijsen thanks for the 0 byte fix!!! \$\endgroup\$
    – Wasif
    Commented 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\$ Commented Oct 5, 2021 at 9:33
  • \$\begingroup\$ @KevinCruijssen oh yes! Coercion is nice \$\endgroup\$
    – Wasif
    Commented 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
    Commented 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
    Commented Sep 21, 2022 at 16:19
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\$
    – rydwolf
    Commented 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
    Commented 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
    Commented 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\$

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\$ Commented Oct 27, 2021 at 0:58
1
\$\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\$
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\$
1
  • 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
    Commented Sep 21, 2022 at 5:16
1
\$\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\$
    – naffetS
    Commented Sep 21, 2022 at 18:44
  • \$\begingroup\$ i was gonna suggest +0 => E but i guess that's not part of the spec anymore \$\endgroup\$
    – naffetS
    Commented Sep 21, 2022 at 18:45
1
\$\begingroup\$

Scala, 106 bytes

Golfed version. Try it online!

def f(s:String)={val g=DateTimeFormatter.ofPattern("HH:mm");LocalTime.parse(s,g).plusMinutes(1).format(g)}

Ungolfed vesion. Try it online!

import java.time._
import java.time.format.DateTimeFormatter

object Main {
  def f(s: String): String = {
    val formatter = DateTimeFormatter.ofPattern("HH:mm")
    val time = LocalTime.parse(s, formatter)
    val newTime = time.plusMinutes(1)
    newTime.format(formatter)
  }

  def main(args: Array[String]): Unit = {
    val s = Array("00:00", "00:02", "05:55", "09:59", "12:49", "20:05", "23:59")
    val r = Array("00:01", "00:03", "05:56", "10:00", "12:50", "20:06", "00:00")

    for (i <- s.indices) {
      val result = f(s(i))
      println(s"${s(i)} => $result (should be: '${r(i)}')")
    }
  }
}

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

Vyxal 3, 21 bytes

24ʀ60ʀẊƛᵛ2Þ0':j]:?Ḟꜝi

Try it Online!

Same as Vyxal 2, might be golfable with modular arithmetic tricks but I doubt it

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

Go, 95 bytes

import."time"
func f(s string)string{F:="15:04"
t,_:=Parse(F,s)
return t.Add(Minute).Format(F)}

Attempt This Online!

\$\endgroup\$
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
    Commented 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
    Commented 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
    Commented Oct 15, 2021 at 9:15
1
2

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.