Created
February 19, 2013 20:40
-
-
Save jpboudreault/4989722 to your computer and use it in GitHub Desktop.
Magical Java Puzzle Solution
http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.Set; | |
// You can't touch this | |
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"); | |
} | |
// Everyone knows that unicorns always like to pat | |
public static class Unicorn { | |
public static boolean pat() { | |
return true; | |
} | |
} | |
// static class named System, how unlikely! | |
private static class System { | |
// static member out, this is getting weird! | |
public static Out out = new Out(); | |
} | |
// needed a name for this class out of inspiration :( | |
public static class Out { | |
private static Set<String> alreadyPrinted = new HashSet<String>(); | |
public static void println(String s) { | |
// this is a crappy printer which never prints twice the same stuff | |
if (!alreadyPrinted.contains(s)) { | |
alreadyPrinted.add(s); | |
// Hammer Time! | |
java.lang.System.out.println(s); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment