26
\$\begingroup\$

You are an employee of Microteque, a leading Silicon Valley startup creating smart microwave ovens for all kinds of strange places. Your customers can get their microwaves printed with patterns to match their kitchens, campers, man caves; even the kitchens of large nation-state facilities have shiny new branded microwave ovens.

Due to the cutting-edge nature of your microwave control board technology, you've ended up having to use the programming language MicrowaveX* and you're working out how to program the time counter. Your counter looks like this:

seven segment display showing 88:88

Your goal is to write a program that takes the input time and translates it into the number of seconds that the microwave needs to run.

As input, the function must take a string in the format ##:## (including the colon at position 3) and return an integer.

Please note that it should also be able to handle more than 60 seconds in the seconds slot.

Finally, due to some pesky regulations, you cannot have your microwave oven run for more than 100 minutes (6,000 seconds)

Sample inputs and outputs:

01:30 --> 90 seconds

00:66 --> 66 seconds

01:99 --> 159 seconds

02:39 --> 159 seconds

99:99 --> 6,000 seconds (capped at 100 minutes due to aforementioned regulations)

*: MicrowaveX happens to be identical to your programming language of choice, but Microteque has rebranded it to sound more appealing to their investors.

\$\endgroup\$
8
  • 6
    \$\begingroup\$ What's the scoring criteria? Code Golf? \$\endgroup\$ May 19, 2020 at 15:41
  • 2
    \$\begingroup\$ 99:99 = 99 min 99 sec is 100 min 39 sec. 100 minutes exactly is 99:60. Is this test case 99:99 intentional or not? (I see it says it would be capped to 100min due to regulations.) \$\endgroup\$ May 19, 2020 at 20:47
  • 3
    \$\begingroup\$ I'd love to program in MicrowaveX for this challenge. Care to link to the website of the language? \$\endgroup\$
    – user92069
    May 20, 2020 at 8:27
  • 1
    \$\begingroup\$ @ Λ̸̸ I think this might be it... scratch.mit.edu \$\endgroup\$
    – AJFaraday
    Jun 3, 2020 at 22:21
  • 1
    \$\begingroup\$ I once had a microwave that, if you entered 00:60 instead of 01:00, it would say "Error" instead of actually starting the timer. I have no idea why, and I've never seen another one like that since. \$\endgroup\$
    – Bbrk24
    Aug 8, 2023 at 19:55

58 Answers 58

1
2
2
\$\begingroup\$

Julia 54 53 52 bytes

f(x)=((i,j)=parse.(Int,split(x,":"));min(60i+j,6e3))

try it online

A single line solution for Julia.

A couple neat tricks here, we use f(x)=, a generic function declaration so we don't have to use the return end or function keywords.

We then use a compound expression like this (code;code;...).

Unfortunately, when using tuple destructuring i,j=, the tuple here must be wrapped in parenthesis for some reason, which adds two bytes.

Edits:

  • 1 byte improvement using 60i instead of 60*i
  • 1 byte improvement by removing trailing whitespace
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 50 48 bytes

-2 bytes thanks to mazzy

$a,$b=$args-split':'
[Math]::min((60*$a+$b),6e3)

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ welcome back :) Try it online! \$\endgroup\$
    – mazzy
    Jun 30, 2020 at 16:56
  • 1
    \$\begingroup\$ @mazzy Thanks, it has been awhile. \$\endgroup\$
    – Veskah
    Jun 30, 2020 at 18:44
2
\$\begingroup\$

C# (.NET Core) 77 bytes

s=>Math.Min(60*int.Parse(s.Substring(0,2))+int.Parse(s.Substring(3,2)),6000);

It assumes that the position of the colon is always 3

Try it online

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

C# (.NET Core), 76 bytes

s=>Math.Min(s.Split(':').Select(int.Parse).Aggregate((e,i)=>(e*60+i)),6000);

Try it online

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to the site and nice first answer! Be sure to check out our tips for golfing in C# page for any improvements you can find! \$\endgroup\$ Jul 3, 2020 at 15:03
  • 1
    \$\begingroup\$ Welcome. Nice first answer! You don't have to count the trailing ; for lambda functions in C#/Java, so that can move to the footer in your TIO. As for golfs, the 6000 can be 6e3 for -1 (the return-type changes from int to double though). :) Enjoy your stay, and good luck with future challenges. \$\endgroup\$ Jul 3, 2020 at 15:14
2
\$\begingroup\$

Java (JDK), 67 bytes

t->Math.min(6e3,new Long((t=t[0].split(":"))[0])*60+new Long(t[1]))

Try it online!

Credits

  • -4 bytes thanks to Kevin Cruijssen by changing the input type to an array of Strings.
\$\endgroup\$
2
2
\$\begingroup\$

V (vim), 18 bytes

f:i*60<Esc>lr+0C<c-r>=<c-r>"
<Esc>

Try it online!

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

Scala, 66 61 bytes

For those who like to live dangerously and assume things beyond a method's contract...

io.StdIn.readLine split ":"map(_.toInt)reduce(_*60+_)min 6000

reduce is not guaranteed to work from left to right, but it seems to do so by default.

Try it in Scastie

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

Io, 52 bytes

Port of the Python answer.

method(x,doString(x split(":")join("*60+"))min(6e3))

Try it online!

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

R, 46 bytes

`:`=c;min(6e3,eval(parse(t=scan(,"")))%*%60:1)

Try it online!

Just redefine : so that we can just use eval to concatenate the hours and minutes together, and do a dot product with c(60,1) to get the seconds.

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

Dart, 88 bytes

import'dart:math';f(s)=>s.split(":").map(int.parse).reduce((v,e)=>min<num>(v*60+e,6e3));

Try it online!

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

Julia, 71 bytes

 function f(x)
        m,s=parse.(Int,split(x,":"))
        return min(m*60+s,6e3)
 end

Try it online!

The part which was most interesting to me here, is the parse.() which parses each element in the array. Note the dot operator before the parentheses.

Could have knocked off one more byte by putting show() in the function instead of return. But, the footer or output box, on tio, wind up a bit of a mess due to the lack of new lines from show.

 function f(x)
        m,s=parse.(Int,split(x,":"))
        show(min(m*60+s,6e3))
 end
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to CGCC! It seems like you can remove some of the whitespace: Try it online! \$\endgroup\$
    – ovs
    May 21, 2020 at 15:20
  • \$\begingroup\$ Thanks @ovs, I spent a little while trying to figure out how to make it one line or maybe use the -> anonymous function. But, I don't know Julia that well (this is honestly the first thing I've ever written in Julia.) This is also my first code golf. Should I put you in as current lead? \$\endgroup\$
    – MorrisMoss
    May 22, 2020 at 18:32
1
\$\begingroup\$

The types and the unwrap make this longer than it could be otherwise.

Rust, 121 bytes

use std::cmp::min;fn f(s:&str)->i32{let x:i32=s[..2].parse().unwrap();let y:i32=s[3..].parse().unwrap();min(x*60+y,6000)}

Try it online!

Explanation: 6000 had to be used because 6e3 is considered a float. 6e3 as i32 is longer than simply using 6000. In production code I would have written it 6_000 but saving the character is the game.

min is not imported by default. So we have to pay for that import.

parse does the work of other languages type casting. But since it could fail we also need the unwrap. String to int conversions are costly in golf for Rust. Using ? operator could in theory save us the unwrap but then the type for the return needs to be much longer: Result<i32, std::num::ParseError>. That also means the collect has to know about the Result too. We also have to wrap the final return in Ok(). So not much gain there.

I looked at using split but it ends up being longer due to working to get the tuple back out of the resulting iterator.

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

Python 3.8 (pre-release), 56 bytes

lambda x:min(int((k:=x.split(":"))[1])+60*int(k[0]),6e3)

Try it online!

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

Golfscript, 28 bytes

":"/~~\~60*+.6000.@<{\}{}if;

Try it online!

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

C# (.NET Core) 88 87 bytes

s=>{var x=s.Split(':').Select(int.Parse).ToList();return Math.Min(60*x[0]+x[1],6000);};

Try it online

Removed trailing newline for 1 byte saving

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

JavaScript (V8), 47 bytes

i=>Math.min(6e3,(i[0]+i[1]-0)*60+(i[3]+i[4]-0))

Call the function with the input string

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

ARBLE, 31 bytes

min(6e3,sub(s,1,2)*60+sub(s,4))

Try it online!

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

Go 1.21+, 93 92 bytes

import."fmt"
func f(t string)int{var m,s int
Sscanf(t,"%d:%d",&m,&s)
return min(6e3,m*60+s)}

Attempt This Online!

Uses the brand new builtin min function to save 13 bytes.

Go 1.20.x, 106 104 bytes

import."fmt"
func f(t string)int{var m,s int
Sscanf(t,"%d:%d",&m,&s)
o:=m*60+s
if o>6e3{o=6e3}
return o}

Attempt This Online!

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

AWK, 30 bytes (27 bytes + 3 penalty for -F:)

$0=6000<(a=$1*60+$2)?6000:a

Try it online!

Pretty straightforward one... Relies on -F: to shorten the code needed to access mins/secs components of the input.

         a=$1*60+$2          - temp variable w/ simple calc of time
$0=6000<(          )?6000:a  - ternary to cap limit at 6000
\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 10 bytes

':/60ḋ«ç⁷Ọ

Try it online!

Explanation

':/60ḋ«ç⁷Ọ  '# Implicit input
':/         '# Split it on ":"
   60ḋ       # Convert from base 60
      «ç⁷    # Push 6000
         Ọ   # Dyadic minimum
             # Implicit output
\$\endgroup\$
0
\$\begingroup\$

Pure Bash, 35 bytes

echo $[s=${1/:/*60+},s>6000?6000:s]

Try it online!

Pure Bash, 36 bytes

echo $[(r=${1/:/*60+})>(b=6000)?b:r]

Try it online!

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

Python, 95 Bytes

lambda i: f"Lower"if(int(i[0:2])*60)+int(int(i[3:5]))>6000else(int(i[0:2])*60)+int(int(i[3:5]))

Example Input:

76:92 # String input

Example Output:

4652 # Number of seconds

Note: You do have to call the function and print it separately

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Aug 13, 2023 at 17:26
  • \$\begingroup\$ I believe int(int(i[3:5])) could be shortened to remove the redundant int call, right? \$\endgroup\$ Aug 13, 2023 at 17:34
  • \$\begingroup\$ Anonymous functions are allowed, so you can remove the f= part to reduce it by 2 bytes. \$\endgroup\$ Aug 13, 2023 at 18:30
0
\$\begingroup\$

Desmos, 43 bytes

Takes input in the form of a ASCII code array. Use this to convert testcases to ASCII codes.

f(S)=min(total([600,60,0,10,1](S-48)),6000)

Try it on Desmos!

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

Itr, 15 bytes

ê©àé2+à©·+6000m

online interpreter

Explanation

ê©àé2+à©·+6000m ; implicit input
ê               ; split input into two parts '##:' and '##'
 ©              ; evaluate the second part to get the number of seconds
  à             ; swap
   é            ; split of the last character ':'
    2+          ; add 2 to ':' (ascii 58) giving 60
      à         ; swap
       ©        ; evaluate the number of minutes
        ·       ; multiply minutes by 60
         +      ; add seconds
          6000  ; literal 6000
              m ; minimum
                ; implicit output
\$\endgroup\$
0
\$\begingroup\$

YASEPL, 43 bytes

=s=1'±colon!¥0,1*60=m¥1,1!+m}2,6000$6000`1<

explanation:

=s=1'±colon!¥0,1*60=m¥1,1!+m}2,6000$6000`1<           packed
=s                                                    set total time in seconds (S)
  =1'                                                 get user input and set it to var1
     ±colon                                           split it by ":"
           !¥0,1*60                                   set S to var1[0] (minutes) and multiply it by 60
                   =m¥1,1!+m                          set M to var1[1] (seconds) and add it to S
                            }2,6000$6000`1            if S > 6000, set it to 6000
                                          <           print S once done                                 
\$\endgroup\$
0
\$\begingroup\$

PARI/GP, 59 bytes

f(x)={s=strsplit(x,":");min(60*eval(s[1])+eval(s[2]),6000)}

Attempt This Online!

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

Nibbles, 15 nibbles (7.5 bytes)

[6000+*60$;$

Attempt This Online!

Explanation

      *60$   # multiply first part of input by 60
     +    ;$ # add second part of input
[6000        # return lesser of sum and 6000
\$\endgroup\$
0
\$\begingroup\$

Swift, 63 57 bytes

{min(Int(($0+"").prefix(2))!*60+Int($0.suffix(2))!,6000)}

Assign this closure to something and call it with the time in "##:##" format (or just call it directly).

Since a Swift String can't be indexed using an Int (blame Unicode support, though you'd be surprised just how rare String indexing is in well-written, real-life code), I had to use the prefix(_:) and suffix(_:) methods instead.

\$\endgroup\$
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.