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.

All solutions, to include Perl6, are welcome.

Cheers - L~R

* 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.


In reply to Odd Ball Challenge by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.