33
\$\begingroup\$

I'm hungry. Let's microwave something. Given a numerical input of between 1 and 4 digits, output the number of seconds that the microwave should run.

Details

The trick is figuring out if the user is inputting seconds or a combination of seconds and minutes. The ones and the tens places should be interpreted as seconds and the hundreds and thousands places should be minutes. For example, the value 1234 should be interpreted as 12 minutes, 34 seconds and 9876 should be 98 minutes, 76 seconds. Typing 130 and 90 should both result in a cook time of 90 seconds.

Here are a few other inputs and outputs:

  • 1 = 1
  • 11 = 11
  • 111 = 71
  • 1111 = 671
  • 9 = 9
  • 99 = 99
  • 999 = 639
  • 9999 = 6039

Rules

This is , so the shortest program in bytes wins. Standard loopholes are not allowed. The winning entry must return the right answer when given any integer input from 1 to 9999.

\$\endgroup\$
  • \$\begingroup\$ @WheatWizard, I'm happy to edit the question. Do you have a suggestion for what I should say in the Details section? Maybe I could make this sentence clearer: "The ones and the tens places should be interpreted as seconds and the hundreds and thousands places should be minutes." \$\endgroup\$ – Andrew Brēza Nov 11 '17 at 4:34
  • \$\begingroup\$ @WheatWizard I just added more detail, let me know if you think I should add more. \$\endgroup\$ – Andrew Brēza Nov 11 '17 at 4:35
  • \$\begingroup\$ Does that work with an input of 9876? \$\endgroup\$ – Andrew Brēza Nov 11 '17 at 4:37
  • 1
    \$\begingroup\$ Strange, I was about to sandbox this exact challenge haha \$\endgroup\$ – FlipTack Nov 11 '17 at 10:46
  • \$\begingroup\$ What would the output be for 190? \$\endgroup\$ – OldBunny2800 Nov 12 '17 at 17:49

30 Answers 30

21
\$\begingroup\$

Python 2, 19 bytes

lambda t:t-t/100*40

Try it online!

\$\endgroup\$
9
\$\begingroup\$

Japt, 6 bytes

ìL ì60

Test it online! ìL converts to base-100, and ì60 converts back to base 60, resulting in floor(n/100)*60 + n%100. Also works with hours (10000 -> 3600, the number of seconds in an hour).

\$\endgroup\$
  • 1
    \$\begingroup\$ Exactly what I had :) \$\endgroup\$ – Shaggy Nov 11 '17 at 22:49
7
\$\begingroup\$

C, C++, Java, C#, D : 36 bytes

D : 35 bytes

C : 28 bytes

First time i have an answer that short !

int r(int i){return i/100*60+i%100;}

D can have a special optimization because of the golfy template system :

T r(T)(T i){return i/100*60+i%100;}

C has a special optimization with the implicit int :

r(i){return i/100*60+i%100;}

Code to test

In C ( have to include stdio.h ) :

int main() {
    int testArr[] = {1,11,111,1111,9,99,999,9999};
    for(int i=0;i<8; ++i) {
        printf("%d = %d\n",testArr[i],r(testArr[i]));
    }
    return 0;
}

TIO Link

In C++ ( have to include iostream ) :

int main() {
    std::initializer_list<int> testList{
        1,11,111,1111,9,99,999,9999
    };

    for (auto x : testList) {
        std::cout << r(x) << '\n';
    }
}

Try it online!

In Java :

public class MainApp {

    int r(int i){return i/100*60+i%100;}

    public static void main(String[]a) {
        MainApp m = new MainApp();
        int testArr[] = new int[]{
                1,11,111,1111,9,99,999,9999
        };

        for (int v : testArr) {
            System.out.println(v + " = " + m.r(v));
        }
    }
}

Try it online!

In C#

class Program {
    int r(int i){return i/100*60+i%100;}
    static void Main(string[] args) {
        var p = new Program();
        int[] testArr = new int[8]
        {
            1,11,111,1111,9,99,999,9999
        };
        foreach(int a in testArr) {
            Console.WriteLine(a + " = " + p.r(a));
        }
    }
}

In D ( have to import std.stdio ) ( exactly, i have no idea how to use arrays in D ) :

void main() {
    int[] arr = [1,11,111,1111,9,9,999,9999];
    for(int i = 0; i < arr.length; i++)
        writeln(arr[i]," = ",r(arr[i]));
} 

TIO Link

\$\endgroup\$
  • \$\begingroup\$ The D test code be the footer of this TIO: tio.run/…, and I see you've learned the template system :). (There is a foreach in D, I just forgot how to use it sadly) \$\endgroup\$ – Zacharý Nov 13 '17 at 12:53
  • \$\begingroup\$ C can be golfed to 28 bytes using C89 implicit-int. \$\endgroup\$ – pizzapants184 Nov 13 '17 at 16:23
  • \$\begingroup\$ You should post all these as separate answers. \$\endgroup\$ – MD XF Nov 25 '17 at 18:53
6
\$\begingroup\$

Pyth - 9 8 bytes

Converts input to base 100, then interprets that as a base 60 number.

ijQ*TT60

Test Suite.

\$\endgroup\$
6
\$\begingroup\$

TI-Basic (83 series), 8 bytes

Ans-40int(sub(Ans

Requires OS version 1.15 or higher.

\$\endgroup\$
6
\$\begingroup\$

dc, 10 bytes

?9A~r60*+p

Try it online!

Explanation: in dc when you push sth. on the stack it goes on top

?         # read and push the input number on the stack
9A        # push 100: 9 * 10^1 + A[10] * 10^0 :D
~         # divide 2nd nr. by the top nr., push quotient, then remainder
r60*      # swap top 2 nr., then multiply the top by 60
+p        # add top 2 nr., then print result
\$\endgroup\$
5
\$\begingroup\$

Bash bc + sed, 30 28 bytes

-2 bytes thanks to @seshoumara.

bc<<<0`sed 's/..\?$/*60+&/'`

Try it online!

Takes input from stdin. Went for a more creative approach: inserts *60+ before the last 1 or 2 digits, and prepends a 0 to the beginning to account for inputs with only 1 or 2 digits. The result is then passed to bc.

\$\endgroup\$
  • 1
    \$\begingroup\$ If you remove -r and use \?, you can loose 2 bytes. \$\endgroup\$ – seshoumara Nov 11 '17 at 7:08
3
\$\begingroup\$

C (gcc), 50 bytes

t;f(){scanf("%d",&t);printf("%d",t%100+t/100*60);}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Perl 5, 15+1(-p) bytes

/..$/;$_-=40*$`
  • -l switch not counted because for tests readability

Try it online

\$\endgroup\$
3
\$\begingroup\$

Java 8, 13 bytes

n->n-n/100*40

Port of @ovs' amazing Python 2 formula.

Try it here.

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

JavaScript, 21 bytes

a=>(a/100^0)*60+a%100

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Save 4 bytes by using ovs's trick - a-(a/100^0)*40 \$\endgroup\$ – IanF1 Nov 11 '17 at 8:33
  • 1
    \$\begingroup\$ @IanF1. Thanks, but I think it would be literally stealing their idea. \$\endgroup\$ – user72349 Nov 11 '17 at 11:48
  • \$\begingroup\$ yeah you're right. Too enthusiastic, sorry. \$\endgroup\$ – IanF1 Nov 11 '17 at 11:56
  • 3
    \$\begingroup\$ @ThePirateBay You don't really live up to your name then ;) \$\endgroup\$ – kamoroso94 Nov 11 '17 at 17:06
2
\$\begingroup\$

J, 12 bytes

-40*&<.%&100

It's ovs' Python 2 solution expressed in J. It consist of a hook and a fork:

┌─┬───────────────────────┐
│-│┌──┬────────┬─────────┐│
│ ││40│┌─┬─┬──┐│┌─┬─┬───┐││
│ ││  ││*│&│<.│││%│&│100│││
│ ││  │└─┴─┴──┘│└─┴─┴───┘││
│ │└──┴────────┴─────────┘│
└─┴───────────────────────┘

       %&100  - divides the number by 100
   *&<.       - finds the floor of the left argument and multiplies it to the left arg.
 40           - 
-             - subtracts the result of the above fork from the input 

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Same byte count as 60#.0 100#:]. \$\endgroup\$ – FrownyFrog Nov 11 '17 at 9:37
  • \$\begingroup\$ @FrownyFrog - your solution looks prettier, cheers! \$\endgroup\$ – Galen Ivanov Nov 11 '17 at 9:43
2
\$\begingroup\$

Batch, 23 bytes

@cmd/cset/a%1-%1/100*40
\$\endgroup\$
2
\$\begingroup\$

Haskell, 18 bytes

f t=t-t`div`100*40

Try it online!

Anotha port.

Pointfree solution, 21 bytes

(-)<*>(*40).(`div`40)
\$\endgroup\$
2
\$\begingroup\$

Labyrinth, 19 bytes

?:_100%}#00/_60*{+!

Try it online!

Explanation

?      Read input.
:      Duplicate.
_100%  Mod 100.
}      Move off to auxiliary stack.
#00/   Divide by 100, using the stack depth to get a 1, instead of _1.
_60*   Multiply by 60.
{+     Retrieve the earlier result and add it.
!      Print.

The IP then hits a dead end and starts moving backward. When it reaches the / it attempts a division by zero which terminates the program.

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

Jelly, 5 bytes

As a monadic link (thanks for the heads-up, caird!):

b³ḅ60

Try it online!

... Or as a full program:

bȷ2ḅ60

This can be ported easily to 05AB1E, so:

05AB1E, 5 bytes

тв60β

Try it online!

Simply converts the input integer to base 100 and then converts the result from base 60 to integer. Hence, it is equivalent to Input % 100 + 60 * ⌊Input / 100⌋

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

Excel VBA, 29 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window.

?[A1]Mod 1E2+60*[Int(A1/100)]
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 11 10 bytes

60⊥0 100⊤⊢

Try it online!

How?

0 100⊤ - encode in base 100, stopping at the second LSB, effectively producing n ÷ 100, n % 100.

60⊥ - decode in base 60

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

PARI/GP, 16 bytes

Straightforward:

n->n\100*60+n%100

Unfortunately this nice method is simply too long to use:

n->[60,1]*divrem(n,100)
\$\endgroup\$
2
\$\begingroup\$

Pushy, 10 9 bytes

Kevin outgolfed me in my own language... (using the approach from ovs' answer)

2dH/40*-#

Try it online!

10 bytes

sjvj60*^+#

Try it online!

s             \ Split input into digits
 jvj          \ Join the first two and the last two
    60*       \ Multiply the first by 60
       ^+     \ Add the values
         #    \ Print

11 bytes

For one byte more we can use the Input % 100 + 60 * ⌊Input / 100⌋ approach:

H2d%}/60*+#

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 9 bytes by creating a port of @ovs' Python 2 answer: 2dH/40*-#. Never programmed in Pushy before, but it seems like a pretty cool language. :) \$\endgroup\$ – Kevin Cruijssen Nov 13 '17 at 13:33
  • 1
    \$\begingroup\$ @KevinCruijssen it's quite a generic stack based language, I guess the only slightly different thing it brings to the table is the double stack... but thank you, and thanks for the golf :) \$\endgroup\$ – FlipTack Nov 13 '17 at 17:37
1
\$\begingroup\$

05AB1E, 9 bytes

т÷60*¹т%+

Try it online!

Explanation:

т÷60*¹т%+

т         // Push number 100
 ÷        // Integer division with the input
  60      // Push number 60
    *     // Multiply with the previous result
     ¹    // Push input
      т   // Push 100 again
       %  // Modulo
        + // Add the first and the second result

Probably there is some trick with base conversions which can be achieved in 05AB1E, but I couldn't find it.

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

bash, 20 bytes

echo $[$1-$1/100*40]

Try it online

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

Retina, 11 bytes

.{100}
60$*

Try it online!

Input and output in unary. The test suite converts from and to decimal for convenience.

Doing this kind of base conversion for only up to two digits is surprisingly simple to do in unary. We just match runs of 100 1s and replace them with 60 1s. Anything that's left over would correspond to the last two digits in the decimal representation and remains unchanged.

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

Alice, 19 bytes

/o
\i@/.'d%~'d:'<*+

Try it online!

Explanation

Too bad I removed divmod from the language, I guess...

/o
\i@/...

This is just the usual framework for linear programs with decimal I/O operating purely in Cardinal (arithmetic) mode.

.     Duplicate input.
'd%   Mod 100.
~     Swap with other copy.
'd:   Divide by 100.
'<*   Multiply by 60.
+     Add.
\$\endgroup\$
1
\$\begingroup\$

Milky Way, 10 bytes

':Z/v40*-!

usage: ./mw code.mwg -i 9999

Explanation:

code       explanation                          stack

'          push input to stack                  [input]
 :         duplicate ToS                        [input, input]
  Z        push 100                             [input, input, 100]
   /v      integer division (divide and floor)  [input, ⌊input/100⌋]
     40    push 40                              [input, ⌊input/100⌋, 40]
       *   multiply                             [input, ⌊input/100⌋*40]
        -  subtract                             [input - ⌊input/100⌋*40]
         ! print
\$\endgroup\$
1
\$\begingroup\$

R, 21 bytes

x=scan();x-x%/%100*40

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I created my own solution in R and it was far less elegant than this. \$\endgroup\$ – Andrew Brēza Nov 13 '17 at 13:25
  • \$\begingroup\$ You can cut the scan since most of the answers just assume the variable is already defined. \$\endgroup\$ – Andrew Brēza Nov 28 '17 at 13:58
  • \$\begingroup\$ The rules are so inconsistent with regards to that. One most other challenges you have to have a function or scan to capture the value. \$\endgroup\$ – Mark Nov 29 '17 at 13:30
1
\$\begingroup\$

Common Lisp, 34 bytes

(lambda(n)(- n(*(floor n 100)40)))

Try it online!

Another port of @ovs' formula.

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

REXX, 25 bytes

arg t
say t%100*60+t//100

(Just another translations of @ovs)

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

05AB1E, 7 bytes

т‰ć60*+

Try it online!

Explanation

         command                              current stack
т‰ć60*+  full program. push input implicitly  [1234]
т        push 100                             [1234] [100]
 ‰       push divmod                          [12, 34]
  ć      push head extracted (n[1:], n[0])    [34] [12]
   60*   multiply by 60                       [34] [720]
      +  add and display implicitly           [754]
\$\endgroup\$
0
\$\begingroup\$

Symbolic Python, 66 bytes

___=-~-~_-_
__=___*___
__=__*__*___+___*__
_=_-_/(__+__+__/___)*__

Try it online!


Explanation

Symbolic Python          values

___=-~-~_-_              ___=2
__=___*___               __=2*2=4
__=__*__*___+___*__      __=4*4*2+2*4=32+8=40
_=_-_/(__+__+__/___)*__  _=_-_/(40+40+40/2)*40=_-_/100*40
\$\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.