A cruise control has 3 different options to move the handle to set the speed you want to drive with.

  • Towards you: Adds 1 speed.
  • Upwards: Increases speed to the next multiple of 10 (e.g. 20-->30, 32-->40)
  • Downwards: Decreases speed to the next multiple of 10 (e.g. 20-->10, 32-->30)


  • 2 integers: the first is the starting speed and the second is your desired speed, both non-negative and in any form you like (array, two arguments, etc.)


  • Determine the optimal way of using the handle to reach the desired speed and print out the moves in the correct order.


  • If you have the choice between pulling towards you and going upwards (like from 39 to 40) you can choose either option, but stay with whatever you choose for similar cases
  • You may use any 3 different (preferably visible) symbols to distinguish between the moves in the output (T, U and D for example).
  • The symbols can be seperated by new lines, spaces, etc. but don't have to be

Here are some test cases:

start speed, desired speed  -->  output
30, 40  -->  U
30, 43  -->  UTTT
43, 30  -->  DD
51, 39  -->  DDDTTTTTTTTT
29, 30  -->  T or U
29, 50  -->  TUU or UUU
12, 12  -->  

This is so the shortest answer in bytes wins.

  • \$\begingroup\$ For anyone who wondered, today I noticed my cruise control has actually a "hidden" button to decrease the speed by 1. I was driving wrong the entire time... \$\endgroup\$ – aTastyT0ast Nov 16 '16 at 15:49

JavaScript (ES6), 91 84 75 bytes

Saved 4 bytes thanks to @Neil


Uses 0 for D, 1 for T, and 2 for U.

|improve this answer|||||
  • \$\begingroup\$ (s/10+1|0)*10 == (s/10|0)*10+10 == s-s%10+10. \$\endgroup\$ – Neil Nov 9 '16 at 22:00
  • 1
    \$\begingroup\$ @Neil Thanks, that helps in another spot too! \$\endgroup\$ – ETHproductions Nov 9 '16 at 22:05
  • \$\begingroup\$ You broke f(37,43) which was 2111 but your new code returns 111111. \$\endgroup\$ – Neil Nov 9 '16 at 22:20
  • \$\begingroup\$ @Neil Fixed at a cost of 2 bytes. \$\endgroup\$ – ETHproductions Nov 9 '16 at 22:28

Java, 144 139

Saved 5 bytes thanks to Kevin.

void o(int s,int e){int t=10,x=s/t;System.out.print(s>e?"D":s<e?x<e/t?"U":"T":"");if‌​(s!=e)o(s>e?x*t-(s%t‌​<1?t:0):s<e?x<e/t?(x‌​+1)*t:s+1:0,e);


public static void optimalCruise(int start, int end){

    if(start > end) {
        optimalCruise(start/10*10-(start%10<1?10:0), end);
    } else if(start < end){
        if(start/10 < end/10){
            optimalCruise(((start/10)+1)*10, end);
        } else {
            optimalCruise(start+1, end);
|improve this answer|||||
  • \$\begingroup\$ By making two int variables for 10 and s/10 you can shorten it by 5 bytes: void o(int s,int e){int t=10,x=s/t;System.out.print(s>e?"D":s<e?x<e/t?"U":"T":"");if(s!=e)o(s>e?x*t-(s%t<1?t:0):s<e?x<e/t?(x+1)*t:s+1:0,e); \$\endgroup\$ – Kevin Cruijssen Nov 10 '16 at 8:10
  • \$\begingroup\$ @KevinCruijssen good catch, I'll edit it in \$\endgroup\$ – dpa97 Nov 11 '16 at 16:16

Batch, 175 bytes

@if %s% gtr %d% echo d&set/as=~-s/10*10&goto d
@if %s% lss %e% echo u&set/as=s/10*10+10&goto u
@if %s% neq %d% echo t&set/as+=1&goto t

Fairly straightforward this time. Takes input as command-line parameters, which it saves into s and d. e is d rounded down to the previous multiple of 10. If s is greater than d, then we obviously need to invoke d until s becomes lower than d. Otherwise, we need to check whether s is lower than e; if so, we can invoke u until s equals e. At this point s is now between e and d and we can simply invoke t until we reach d. I looked into for loops but they use inclusive endpoints so would have become too verbose.

|improve this answer|||||

Python, 76 bytes

lambda a,b: "U"*(b//10-a//10)+"D"*(a//10-b//10+(b<a))+"T"*min(b%10,(b-a)%99)
|improve this answer|||||
  • \$\begingroup\$ min(b%10,(b-a)%99) wont always work, for example (a,b)=(132,33) \$\endgroup\$ – Jonathan Allan Nov 10 '16 at 3:41
  • \$\begingroup\$ You have an extra space after b: \$\endgroup\$ – Stephen Aug 9 '17 at 14:38

C, 156 bytes

t,x,y;main(int s,char**a){x=(s=atoi(a[1]))/10,y=(t=atoi(a[2]))/10;if(s^t){for(;y>x;++x)puts("U");for(x+=(s-t>10);x>y;--x)puts("D");for(;t--%10;)puts("T");}}


#include <stdio.h>
#include <stdlib.h>
int t, x, y;
int main(int s, char **a)
    x = (s = atoi(a[1])) / 10,
    y = (t = atoi(a[2])) / 10;

    if (s ^ t) {
        for ( ; y > x; ++x)

        for (x += (s - t > 10) ; x > y; --x)

        for ( ; t-- % 10; )
    return 0;
|improve this answer|||||

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.