Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

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!