Java Puzzle 3: Car

Mar 01, 2012

This java puzzle comes in two parts. You have to solve this part to see the next one.
To warm up the engine, we start with the easiest one. Part two will be harder.

This car crashes if you accelerate too much. But can you make it go ten times faster than its limit?

package car;

public final class Car {
    private static final int MAX_SPEED = 100;

    private int speed = 0;

    public synchronized void accelerate(int acceleration) {
        if (acceleration > MAX_SPEED - speed)
            crash();
        else
            speed += acceleration;
    }

    public synchronized void crash() {
        speed = 0;
    }

    public synchronized void vroom() {
        if (speed > MAX_SPEED * 10) {
            // The goal is to reach this line
            System.out.println("Vroom!");
        }
    }
}

As a driver, do what needs to be done to push the car over its limit. Anything in your code is allowed; any trick outside the code is not. You must run with -Djava.security.manager, so setAccessible won't work. If in doubt, read the exact rules.

package driver;

import car.Car;

public class Driver {
    public static void main(String args[]) {
        // TODO break the speed limit
        Car car = new Car();
        car.accelerate(1001);
        car.vroom();
    }
}

If you've found the solution, post it into the ugly form over there. There it will be compiled and ran, and if it works, you will be directed to part two. When you've solved it, take a look at part 2.

To be notified of when the solution and next puzzles come out, follow the rss feed or twitter.


4 comments

TPReal wrote on 2012-03-02 00:26

The submission form doesn’t work for me I think. All I get is this:

Execution id car1-120301-222509-25954-89.76.105.59 Please wait… … Done. Output: Compilation successful. Starting program.

And I wait and wait (I know this is Java so patience might be needed) and nothing happens.

TPReal wrote on 2012-03-02 00:29

Oh well, I was expecting some more messages from the system, like: program finished, failed, or something. But I see there’s just the output there (empty for that run). So never mind.

Wouter wrote on 2012-03-02 00:33

I just added a “Program ended.” message. There’s also a line below to indicate that’s the end, but that was a bit too subtle.

Frank wrote on 2012-03-02 22:11

Should the solution always succeed (ie be deterministic)?

Wouter wrote on 2012-03-02 22:30

It doesn’t need a 100% guarantee of working every time. The more reliable, the better. There’s no strict line of what is and isn’t reliable enough.

There is a solution that always seems to work in practice (as far as I know, tested on multiple systems), and does not take any significant time to execute. So that should be your goal.

Michael wrote on 2013-03-19 06:48

Do these puzzles still work? I’m getting an error when trying to submit my (extremely simple) solution.

The error is “The page you were looking for doesn’t exist.”

Wouter wrote on 2013-03-19 07:06

The host running that site that executes the code for you has recently gone down. I’m not going to bother bringing it up again. I just edited the post to reflect that. Thanks for letting me know.

Michael wrote on 2013-03-19 06:50

NVM, just saw that I could get to part 2 w/o submitting