Puzzle Solved in Java