kaif,
I think you misunderstood the challenge and wrote a solution to the riddle instead. You are supposed to start out not knowing how to solve the riddle and write code that tells you how. The bonus was to have the code smart enough to understand whatever it outputted and generate a solution (such as the one you provided).
Let me try and be clearer this time since it was likely my fault. You start out knowing only the rules of riddle. You right code that starts searching for combinations of groupings and weighings that keeps track of what knowledge it has gained along the way. Eventually it has enough information to say:
- Split the 12 balls up into X groups of Y balls
- First, balance group A against group B
- If they balance do ....
- If A is heavier do ....
- etc, etc, etc
I am not real particular in how that information is conveyed but it would be really cool if once the code reaches its conclusion it is smart enough to understand its own output and also generate code to execute the steps.
| [reply] |
I've thought about this particular problem before, and I wrote the kind of program you would like to see, in C++. It was challenging, and somewhat time-consuming, which is probably why no one has responded yet like that; I was going to simply translate my program to Perl, but I think I dropped it in the bit bucket.
I eventually generalized the problem and solved it mathematically, noticing that 12 == (3**3 - 1)/2 - 1. Thus, to me, this is an old case of the following: given the task "write a program to sum the numbers from 1 to $x" after learning about loops, does one write print $x*($x+1)/2 or
for $i ( 1 .. $x ) {
$sum += $i;
}
print $sum;
So, I went the easier route and golfed in a relatively general way; my comparisons "2 9 11 12 <=> 3 5 6 8", etc., are not hard-coded (I could write a much shorter program otherwise) and the computation of the result is clever. I hope that this is okay. I understand your challenge, but I still think that the less-than-240-characters above is an accomplishment. Thank you for the clarification, though. | [reply] [d/l] [select] |
kaif,
I certainly didn't mean to imply I wasn't impressed - I am. I understand this isn't an easy problem and most monks will find it too much work to devote with no tangible reward. I just like to share the problems I think up because some monks do enjoy them immensly.
| [reply] |