06-28-2010, 07:16 AM
|
#4
|
Addict
Posts: 241
Karma: 2617
Join Date: Mar 2009
Location: Greenwood, SC
Device: Kindle 2
|
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";
|
|
|