All,
There is a well known riddle as follows:
You are presented with 12 balls identical in appearance but 1 of the 12 is either heavier or lighter than the other 11. Your task is to identify which is the odd ball and if it is heavy or light. You are only allowed to make 3 weighings using a balance.
I have included a currently* non-working Perl6 solution below because that is not the challenge:
use v6;
my %ball = map { $_ => 1; } 1..12;
Modify_Ball( %ball );
say ~Find_Odd_Ball( %ball );
say %ball.perl;
sub Find_Odd_Ball (%ball) {
my $result_1 = [+] %ball{1..4} <=> [+] %ball{5..8};
given $result_1 {
when 0 {
my $result_2 = [+] %ball{9,10} <=> [+] %ball{1,11};
given $result_2 {
when 0 { return (12, %ball{12} <=> %ball{0}) }
default {
my $result_3 = %ball{9} <=> %ball{10};
given $result_3 {
when -1 { return (9, $result_2 == $result_3 ?
+? -1 :: 1 }
when 0 { return (11, $result_2 * -1 }
when 1 { return (10, $result_2 == $result_3 ?
+? 1 :: -1 }
}
}
}
}
default {
my $result_2 = [+] %ball{1,2,5} <=> [+] %ball{3,6,10};
given $result_2 {
when -1 {
my $result_3 = %ball{1} <=> %ball{2};
given $result_3 {
when -1 { return (1, $result_1) }
when 0 { return (6, $result_1 * -1) }
when 1 { return (2, $result_1 * -1) }
}
}
when 0 {
my $result_3 = %ball{7} <=> %ball{8};
given $result_3 {
when -1 { return (8, $result_1 * -1) }
when 0 { return (4, $result_1) }
when 1 { return (7, $result_1 * -1) }
}
}
when 1 { return %ball{3} == %ball{10} ?? (5, $result_
+1 * -1) :: (3, $result_1); }
}
}
}
}
sub Modify_Ball( %ball is rw ) returns Void {
my $num = int( rand 12 ) + 1;
my $adj = .1 * (int( rand 2 ) ?? 1 :: -1);
%ball{$num} += $adj;
}
Challenge: Start out not knowing how to solve the riddle and write code that tells you how. It should generate a set of groups and measurements that when followed will solve the riddle. I am not real particular in how a human would interpret the output, but bonus points for code that understands its own output and produces code that solves the riddle (such as my example).
How many ways are there to measure 12 balls in groups of two 3 times so that you are guaranteed to know which ball is odd and how it is odd? I think Ovid's AI::Prolog may be the right way to go.
Every time you make a measurement using the balance there are 3 possible outcomes.
- If they balance, the odd ball must be in the group not currently being weighed
- If the right side goes down, either a ball in that group is heavy or a ball on the left side is light
- If the right side goes up, either a ball in that group is light or a ball on the left side is heavy
All solutions, to include
Perl6, are welcome.
* I say currently because I suspect the problem is with Pugs and not with the code.
Update: Clarified challenge to indicate I am not looking for a solution to the riddle but for code that can figure out a solution on its own. That is to say, a solution-generator. See Challenge: Setting Sun Puzzle for an example.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.