Java Puzzle 1: Clowns
Feb 16, 2012Introduction
This is the first in a series of Java puzzles, that put your Java skills to the test, in a challenging and fun way!
A puzzle consists of some given Java code with a line in it that seems to be impossible to reach. It's up to you to find the hole in it, abuse a subtle behavior of Java to make execution reach that line anyways.
There almost aren't any rules; any cheating inside your code is allowed; it is the whole point of the puzzle. [Clarification: Cheating the environment is not]. You must run with the security manager enabled (java -Djava.security.manager
), otherwise it would be too easy (with setAccessible for example). [Update: Use common sense, and see the exact rules if you're in doubt].
The puzzle
How can you fit 20 clowns into a Volkswagen? Two classes are given: an empty Clown class, and a Volkswagen class to which you can add clowns. When you try to add a Clown, it is checked that it isn't already full. But if you just try hard enough, there's always room for some extra clowns...
Write a class that when executed pushes 20 clowns into the little car, and reaches the marked line. Here is one that won't really work, just to get you started:
You can copy-paste the code into your IDE, or get it from github (zip).
If you've solved it, let me know (email or Twitter). If you're the first, you'll get... the honor of being the first. compare it to the solution posted here.
The solution and a new challenge will follow, so stay tuned!
Update: Don't post answers as comments here, they will be blocked by moderation. You may comment, but no spoiler. If you have an alternative solution, post it as a comment on the solution post
Update 2: Added clarification of the rules.
10 comments
A quick and dirty solution would be: private static final int CAPACITY = 20;
I should have mentioned it more explicitly, but you are only allowed to edit the file containing the “You” class, nothing else.
Solution found in 30 minutes. Very nice game.
Are we allowed to use a customer classloader? It sounds too much of a cheat to me.
Good question. You are allowed to create a custom classloader and load any classes in from within your code, for as far as the security manager allows that. But “The goal is to reach this line” only applies to that line in the real Volkswagen class, not a copy of it (which will need a different name I think) in another classloader.
You are supposed to run just
java -Djava.security.manager you.You
, adding extra options tojava
(for example to use another classloader, or a non-standard security policy) is not allowed.I am not really expert in java so waiting for solution. :P Would it be posted here?
Yes, it’s coming soon.
I need easier puzzles! Seems that only one semester of java basics a couple of years ago just isn’t enough to solve this ;)
Yeah, I’ll also have some easier ones. Not the next one, but the third one probably…
No please, no easier puzzles. More difficult! The web has tons of easy Java puzzles.
I have to agree with Heinz on this one.
Any lateral thinker with solid Java experience (or broad reading) should be able to get the general idea within a short period of time.
It was a good puzzle. Took me 30-45 minutes (I wasn’t tracking too closely).
I’m a Volkswagen with 20 clowns! ;-)
Not really hard when you have done the 2nd puzzle first.
This is brilliant! I have not solved it myself, so nothing to be proud of, but it was a pleasure understanding it anyhow. Thx a lot!
Would it be considered a legit solution to use serialize-adjust_byte_code-deserialize trick? The class file is not touched and the runtime Volkswagen instance would be real…
Yes, anything in your code is allowed. But you’ll have some trouble with that trick: Volkswagen is not serializable.
Excellent puzzle, takes one to the depth of the fundamentals.