### Re: Odd Ball Challenge

by kaif (Friar)
 on Jun 23, 2005 at 21:51 UTC ( #469536=note: print w/replies, xml ) Need Help??

in reply to Odd Ball Challenge

Challenge accepted. For better or worse, I am using "math" when it comes to this problem (this problem clearly generalizes).

```sub c{print"@{\$_[0]} <=> @{\$_[1]}: -/+1 if left/right heavier, 0 if eq
+ual: ";<>}
sub b{\$_[0]<13?\$_[0]:26-\$_[0]} sub a{2>(\$_[0]+(\$_[0]>11))%4} sub d{sor
+t{\$a<=>\$b}
map b(\$_),grep{a(\$_)&&\$_[1]==int(\$_%3**\$_[0]/3**\$_[0]*3)}1..25}\$;+=3**
+\$_/3*(1+c(
[d(\$_,0)],[d(\$_,2)]))for 1..3;print b(\$;)." is ".(a(\$;)?"heavi":"light
+")."er\n";

Replies are listed 'Best First'.
Re^2: Odd Ball Challenge
by Limbic~Region (Chancellor) on Jun 23, 2005 at 22:09 UTC
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.

Cheers - L~R

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.

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.

Cheers - L~R

Create A New User
Node Status?
node history
Node Type: note [id://469536]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2021-05-07 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (91 votes). Check out past polls.

Notices?