Wednesday, July 29, 2009

Puzzle 45 - Solution

There are many solutions to the puzzle and I've have tried to broadly classify the solution into variuos groups.
These don't necessary cover all the solution...

1. Run the program by passing two arguments to it.
First time both the arguments are same, the next time both the arguments are different
someType1 & someType2 = int
someValue1 = Integer.parseInt(args[0]);
someValue2 = Integer.parseInt(args[1]);
First Run: java com.twisters Equal_Unequal 1 1

Second Run: java com.twisters Equal_Unequal 1 2

2. Run the program by passing just one parameter.
Depending on a java principal get the program to print true and false.


a. Use the caching property of the wrapper classes for values below 127.
someType1 & someType2 = Float
someValue1 = Float.parseFloat(args[0]);
someValue2 = Float.parseFloat(args[0]);

First Run: java com.twisters Equal_Unequal 1
Second Run: java com.twisters Equal_Unequal 1024

b. Use the NAN property of floats
someType1 & someType2 = float
someValue1 = Float.parseFloat(args[0])/Float.parseFloat(args[0]);
someValue2 = someValue1;

First Run: java com.twisters Equal_Unequal 1.0
Second Run: java com.twisters Equal_Unequal 0.0

3. Change an External condition.
a. Create or delete a file on the file system
someType1 & someType2 = boolean
someValue1 = isFileExists(c:\test1.txt)
someValue2 = isFileExists(c:\test2.txt)

First Run: java com.twisters Equal_Unequal /*Create both the files so someValue1 & someValue2 are both true*/
Second Run: java com.twisters Equal_Unequal /*Create just one files so only one of someValue1 or someValue2 is true*/

b. Change an environment variable

c. change the time of the machine.

4. Use a Random generator in the code.
There are many ways to generate random results. Though technically not absolutely accurate - one of the runs in bound to generate a true first and a false the second time.

5. Byte code manipulation.
You can't recompile the code but you can always go ahead and change the byte code ;)

This should cover up most of the solutions - though I would suggest having a look at the comments for a real wide varity of solutions.
I would post the scores for this question as a separate post this weekend - watch out for it!!

No comments:

Post a Comment

Solution for this question?