20
\$\begingroup\$

Related: Program my microwave oven and Generate lazy values.

My colleague is so lazy that he doesn't even bother to move his finger when programming the microwave oven. (This is actually true!)

Help him find the microwave input that gives the time closest to what he wants, but where all digits are the same. If two inputs result in the same time difference from the desired time, choose the one with fewer digits. If both have the same number of digits, choose the lesser – so he doesn't have to wait so long.

Input is the integer that a perfectionist would enter, e.g. 430 is 4 minutes and 30 seconds while 100 and 60 each is 1 minute. It will be greater than 0 and will not exceed 9999.

Output must an integer, e.g. 444 is 4 minutes and 44 seconds and 55 is 55 seconds.

Both input and output may only be in simple seconds (no minutes) if the total time is below 1 minute and 40 seconds.

This is , so your code must be as short as possible.

Test cases:

  30 →   33
  60 →   55
  70 →  111
  90 →   88
 100 →   55
 101 →   66
 120 →   77
 130 →   88
 200 →   99
 201 →  222
 500 →  444
 700 →  666
1000 →  888
1055 →  999
1056 → 1111
1090 → 1111
\$\endgroup\$
5
  • 5
    \$\begingroup\$ I like the half-lazy approach. Just keep mashing the "add 30 sec" button until it's there :D \$\endgroup\$
    – Geobits
    Commented Feb 23, 2016 at 2:53
  • \$\begingroup\$ @Geobits Awful lot of presses until 11:30. Anyway, the fact is that he uses the method I wrote above... :-D \$\endgroup\$
    – Adám
    Commented Feb 23, 2016 at 2:54
  • 2
    \$\begingroup\$ Yea, I usually just type it in for anything over a few minutes. It's a delicate tradeoff between number of presses and finger-travelling distance ;) \$\endgroup\$
    – Geobits
    Commented Feb 23, 2016 at 2:56
  • \$\begingroup\$ Is input/output in # of seconds allowed? \$\endgroup\$ Commented Feb 23, 2016 at 3:06
  • 3
    \$\begingroup\$ My colleague is so lazy that he doesn't even bother to move his finger when programming the microwave oven. Didn't expect any less from someone working at Dyalog APL headquarters... :) \$\endgroup\$
    – lynn
    Commented Feb 24, 2016 at 16:18

3 Answers 3

4
\$\begingroup\$

Jelly, 26 bytes

bȷ2ḅ60
³ÇạÇ,
9Rẋ€4R¤ḌFÇ€ṂṪ

Explanation:

bȷ2ḅ60             f(x) = x tobase 100 frombase 60
³ÇạÇ,              g(x) = (abs(f(arg) - f(x)), x)
9Rẋ€4R¤            main(arg) = [1..9] repeat each with [1..4],
       ḌF           then get digits and flatten,
         ǀ         then map g,
           Ṃ        then minimum,
            Ṫ       then last element.

Try it online!

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

JavaScript (ES6), 112 bytes

n=>{c=n=>n*3+n%100*2;d=n=c(n);for(r=i=0;i<1e4;i++)/^(.)\1*$/.test(i)&(m=c(i)-n,m<0?m=-m:m)<d&&(d=m,r=i);return r}

Uses a helper function c which calculates five times the actual number of elapsed seconds.

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

Dyalog APL, 37 bytes

{⍵⊃⍨⊃⍋⊃|-/(60⊥0 100∘⊤)¨⎕⍵}⍎¨,⎕D∘./⍨⍳4

⍳4 1 2 3 4
⎕D "0123456789"
∘./⍨ repetition table (like a multiplication table, but where each cell contains B repetitions of A instead of A×B)
, make table into list of strings
⍎¨ make each string into number (Now we have a list off all possible results.)
{} function where the argument is represented by
⎕⍵ precede argument with prompted input
( apply to each of the two (the argument and the list)...
0 100∘⊤ convert to base-100
60⊥ convert from base-60
-/ calculate the difference between the two
| absolute value
extract list (because -/ encapsulated its result)
sort order (Does not sort, only returns the order in which to place the arguments to achieve ascending order. If two elements are equal, they stay in current order. Since our list has elements of increasing length, this takes care of ties.)
the first one, i.e. the one with smallest absolute difference from the input ⍵⊃⍨ take that element from the argument list (the list of possible results)

Thanks to the colleague in question for shaving off one byte.


Note: I did not have any solution at the time of posting the OP.

\$\endgroup\$

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.