This pattern seems to work:
- - - - L L R L R L R R - L L L R R L - - - R R L R - L R - R R - L L -
I wrote a proof of theory script which uses this fixed strategy technique. I don't know if it satisfies all of the requirements of the challenge, but it does appear to work.

Each of the three fixed weighs results in either 0, 1, or -1. These weigh results are joined together to create a unique key for each ball/weight combination which are then used to generate a lookup table that can then be used to quickly find the oddball and its weight in any given set.

use strict; use warnings; my $ballfinder = My::Oddball->new( [5,6,8,10], [7,9,11,12], # L/R set 1 [2,3,4,7], [5,6,11,12], # L/R set 2 [1,4,10,11], [2,5,7,8], # L/R set 3 ) or die "Bogus ball arrangement"; # Now we have a valid table, let's use it to find some random oddballs for (1..5){ my $weight = (0.9, 1.1)[ int(rand(2)) ]; my $oddball = (1..12)[ int(rand(12)) ]; my $balls = [ qw/ 0 1 1 1 1 1 1 1 1 1 1 1 1 / ]; $balls->[$oddball] = $weight; print "Randomly chosen ball: $oddball, ".($weight < 1 ? 'light' : 'he +avy')."\n"; print $ballfinder->detect($balls)."\n\n"; } package My::Oddball; sub new { my ( $class, $L1, $R1, $L2, $R2, $L3, $R3 ) = @_; my $table = { }; # Iterate through all possible combinations: balls 1-12, light & heavy foreach my $weight qw/ 0.9 1.1 / { for my $oddball (1..12){ my $balls = [ qw/ 0 1 1 1 1 1 1 1 1 1 1 1 1 / ]; $balls->[$oddball] = $weight; my $key = canon_key( balance( [ @$balls[@$L1] ], [ @$balls[@$R1] ] ), balance( [ @$balls[@$L2] ], [ @$balls[@$R2] ] ), balance( [ @$balls[@$L3] ], [ @$balls[@$R3] ] ), ); # If the table is valid, we should get a unique key # for each ball/weight combination return if exists $table->{$key}; $table->{$key} = "Ball $oddball is ".($weight < 1 ? "light" : "he +avy"); } } return bless { table => $table, sets => [ $L1, $R1, $L2, $R2, $L3, $R +3 ] }, $class; } sub detect { my ($self, $balls) = @_; my ( $L1, $R1, $L2, $R2, $L3, $R3 ) = @{$self->{sets}}; my $key = canon_key ( balance( [ @$balls[@$L1] ], [ @$balls[@$R1] ] ), balance( [ @$balls[@$L2] ], [ @$balls[@$R2] ] ), balance( [ @$balls[@$L3] ], [ @$balls[@$R3] ] ), ); return $self->{table}{$key}; } sub canon_key { join( '_' => @_ ) } sub balance { my ( $setA, $setB ) = @_; return weigh($setA) cmp weigh($setB); } sub weigh { my ($set) = @_; my $sum = 0; $sum += $_ for @$set; return $sum; }

In reply to Re^2: Odd Ball Challenge by djohnston
in thread 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.