ZeroTurnaround Blog

Product Updates, Company News & Fun

Magical Java Puzzle: “Pat The Unicorns”

It is almost spring in some places, and believe it or not, several Unicorns have already been spotted by our Magical Creature Department. I started thinking that with all these freaking Unicorns around, wouldn’t it be nice to make contact? So I thought up a puzzle: Can we pat them only once? Let’s start with the following piece of code:

public class MagicalLand {
  public static void main(String[] args) {
    for (int i = 0; i < (Math.random() * 500) + 2; i++) {
      if (Unicorn.pat()) {
        System.out.println("UNICORN #1: PAT THIS UNICORN ONCE");
      }
    }
 
    for (int i = 0; i < (Math.random() * 500) + 2; i++) {
      if (Unicorn.pat()) {
        System.out.println("UNICORN #2: PAT THIS UNICORN ONCE");
      }
    }
    System.out.println("END OF PROGRAM");
  }
}

There are two Unicorns hidden in this puzzle, but the magic surrounding the area makes it difficult to tell them apart. Your goal is to pat both Unicorns only once and then leave the magical land. Let me give you an example run.

# example run
javac MagicalLand.java
java MagicalLand
UNICORN #1: PAT THIS UNICORN ONCE
UNICORN #2: PAT THIS UNICORN ONCE
END OF PROGRAM

There are two rules. The first rule is that the body of the MagicalLand is not to be tampered with! The second rule is that you are not allowed to deflect the program flow and just print out the expected output directly. After all, these Unicorns produce top-quality meat, and we think that they’ve earned a pat!

If you think you know a cool solution drop us an email with a link to the gist to [Solutions are no longer accepted]. Have a great subject for the email, easier for us to find the correct ones, for example “Tasty Unicorn Meat in My Face”. But don’t be a jerk: leave out your complete solutions in the comments please!

We’ll go over the submissions some time next week, and we’ll follow up with a post that shows some cool solutions and prizes (I’ll have to check with our Marketing Director if we’ll just give you a pat on the back like the unicorns get, or some free software, or a private helicopter made out of gold). More on that later.

Good luck!

  • -

    You’re making it too easy, not changing the body means we can define our own imports -> our own Math class, right?

  • http://twitter.com/shelajev Oleg Šelajev

    I think the idea was that you cannot change Magical Land at all, so your solution should reside outside of this file.

  • http://www.facebook.com/bernerbits Derek Berner

    Submitted. I must be spending too much time in Java-land… Thanks for the brain-teaser!

  • Hannu Leinonen

    I submitted my poetic unicorn.

  • mike

    You do not say anything about Math class in MagicalLand :) If I create my Math class with a random() method that returns -0.002, then you will have a single print for every for.
    I think it will be better to put the hole class (with imports)

  • Alec Lanter

    My unicorns aren’t poetic, they’re just 1000 pounds of enraged horse with a horn at one end.

  • Alec Lanter

    I’m going to stop submitting before I get myself disqualified for spam. O.o

  • http://www.facebook.com/sergii.maliarov Sergii Maliarov

    That’s a solution that you should not leave in the comments ^_^

  • Neil

    What a clever and dumb answer at the same time.
    Way to go and spoil the puzzle Mike.

  • mike

    The comment was not an answer. It was about an incomplete specification.

  • Stas Shymov

    waiting for golden helicopter…

  • Wouter Blancquaert

    Pata 1, Pata 2, Bingo :D

  • Ivan

    So obvious and yet so hard to see.

  • gorlok

    I sent my solution. Nice challenge.

  • http://jmora.tumblr.com jmora

    I’ve sent my solution but I’m confused about this: “But don’t be a jerk: leave out your complete solutions in the comments please!”.

    Should I write it here? It seems to be the only “rule”.

  • demonh3x

    You should not.

  • http://profiles.google.com/matthias.bolliger Matthias Bolliger

    Any solution post yet? Curious…

  • http://www.zeroturnaround.com/ Toomas Römer
  • http://profiles.google.com/matthias.bolliger Matthias Bolliger

    Thank you!