Quote:
Originally Posted by clarknova
Spoiler:
I dunno why, but my 'puter tells me it's white every time.
Code:
#!/usr/bin/perl
use List::Util 'shuffle';
my (@pot,@pile);
for (my $i=0; $i<300; $i++) { # Add 300 black beans to the pile
push (@pile,'B');
}
for (my $i=0; $i<150; $i++) { # Add 150 black beans to te pot
push (@pot,'B');
}
for (my $i=0; $i<75; $i++) { # Add 75 white beans to the pot
push (@pot,'W');
}
while (scalar(@pot) > 1) { # Loop until one bean remains in the pot.
@pot = shuffle(@pot); # Stir the pot.
my $b1 = pop(@pot); # Grab Bean 1 from the pot
my $b2 = shift(@pot); # Grab Bean 2 from the pot
if ($b1 eq 'B') { # Bean 1 = Black Bean
push(@pile,$b1); # Put it in the Pile
push(@pot,$b2); # Throw the other in the pot (could be w or b)
} elsif ($b2 eq 'B') { # Bean 2 = Black Bean
push(@pile,$b2); # Put it in the Pile
push(@pot,$b1); # Throw the other in the pot (it's white)
} else { # Both are White, discard them.
$b1 = pop(@pile); # Grab one from the pile.
push(@pot,$b1); # Put it in the pot.
}
} # Rinse, Wash, Repeat.
print "Last Bean: $pot[0].\n";
|
I admire the coding effort, but it is possible to solve this without the the use of a computer. Even if you do have the right answer.