I'm having trouble understanding how it even compiles. Isn't
when -1 { return (9, $result_2 == $result_3 ?
+? -1 :: 1 }
a syntax error (mismatched parentheses)? If so, there are a few lines like that. Also, one line refers to
when 0 { return (12, %ball{12} <=> %ball{0}) }
where 0 should probably be 1. Finally, I confirm all of djohnston's results by translation to Perl 5 (for example, just imagine ball 1 is heavier and run through the code mentally --- it indeed says something about either ball 3 or 5). Pardon the ugly code; the Perl 6 is certainly prettier. But as Limbic~Region says, solving the riddle isn't the challenge, right?
use Test::More tests => 24;
use warnings;
for( 1 .. 12 ) {
%ball = map { $_ => 1; } 1..12;
for $d ( -1, +1 ) {
$ball{$_} = 1 + $d * 0.1;
is_deeply( [$_, $d], [Find_Odd_Ball()], "($_, $d)" );
}
}
sub Find_Odd_Ball {
my $result_1 = $ball{1} + $ball{2} + $ball{3} + $ball{4} <=> $ball
+{5} + $ball{6} + $ball{7} + $ball{8};
if( $result_1 == 0 ) {
my $result_2 = $ball{9} + $ball{10} <=> $ball{1} + $ball{11};
if( $result_2 == 0 ) {
return (12, $ball{12} <=> $ball{1});
} else {
my $result_3 = $ball{9} <=> $ball{10};
if( $result_3 == -1 ) {
return (9, $result_2 == $result_3 ? -1 : 1 );
} elsif ($result_3 == 0 ) {
return (11, $result_2 * -1);
} else {
return (10, $result_2 == $result_3 ? 1 : -1 );
}
}
} else {
my $result_2 = $ball{1} + $ball{2} + $ball{5} <=> $ball{3} + $
+ball{6} + $ball{10};
if( $result_2 == -1 ) {
my $result_3 = $ball{1} <=> $ball{2};
if( $result_3 == -1 ) {
return (1, $result_1);
} elsif( $result_3 == 0 ) {
return (6, $result_1 * -1);
} else {
return (2, $result_1 * -1);
}
} elsif( $result_2 == 0 ) {
my $result_3 = $ball{7} <=> $ball{8};
if( $result_3 == -1 ) {
return (8, $result_1 * -1);
} elsif( $result_3 == 0 ) {
return (4, $result_1);
} else {
return (7, $result_1 * -1);
}
} else {
return $ball{3} == $ball{10} ? (5, $result_1 * -1) : (3, $
+result_1);
}
}
}
-
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.