Perlish solutions to problems
Nov. 9th, 2005 04:04 pmWell, that problem I mentioned.
I cracked and went perl hacker on it.
Result?
10 x 4 point guns vs. 50 '5 point soak' ships (worst possible situation for them).
Fleet is reduced to 0 in an average of 36 'rounds'. (869 ships destroyed first round, out of 1000)
40 1 point guns, vs 50 '5 point soak' gives us an average of 195 rounds. (61 ships destroyed first round).
I think that's pretty conclusive, if not quite what I had expected. (And yes, I was assuming that ships can fully repair, since the 'model' involved a gun firing on a target for 30 seconds, and then 'switching', meaning it can retreat, and return when fully healed up. (there's ways to prevent them retreating, but I'm ignoring them because they also randomly target, and are applicable to both)
However I shall return to pondering the ponderableness of this problem, because I'm sure there's got to be a formula for it ;)
I cracked and went perl hacker on it.
#!/bin/perl
use warnings;
use strict;
my $number_of_ships = 50;
my $damage_soaked = 5;
my $number_of_guns = 40;
#my $number_of_guns = 10;
my $gun_damage = 1;
#my $gun_damage = 4;
my $iterations = 0;
my $result_sum = 0;
my $total_loops = 1000;
my $first_round_kills =0;
my @ships;
for ( my $loopcount=0; $loopcount <$total_loops; $loopcount++)
{
$iterations=0;
#create a 'fleet array' of ships
for ( my $n = 1; $n <= $number_of_ships; $n++ )
{
push @ships, $damage_soaked;
}
print "starting fleet ", join(":", @ships ), "\n";
#repeat till they're all GONE.
while ( $#ships >=0 )
{
$iterations++;
for ( my $gnum =1; $gnum <= $number_of_guns; $gnum++ )
{
#randomly distribute gunnery damage (they all target select at once, so
#overkill is possible
$ships[rand($#ships)] -= $gun_damage;
}
# print $iterations, "Outcome ", join (":", @ships ), "\n";
my @newshiplist;
#now take the ship list. Delete anything that's on 0 or less, and then
#assume that those that are damaged have warped out and repaired.
foreach my $aship ( @ships )
{
# print "S:", $aship, "\n";
if ($aship > 0) { push @newshiplist, $damage_soaked }
}
@ships = @newshiplist;
print $#newshiplist+1, " ships survive\n";
if (( $#newshiplist+1 < $number_of_ships ) and ( $iterations == 1 ))
{ $first_round_kills += $number_of_ships - $#newshiplist - 1}
}
print $iterations, " rounds to destroy ", $number_of_ships, " ships using ", $number_of_guns, " guns doing ", $gun_damage, " damage each.\n";
$result_sum += $iterations;
}
print "Average iterations to destroy = ", $result_sum / $total_loops, "\n";
print $first_round_kills, "first round kills\n";
Result?
10 x 4 point guns vs. 50 '5 point soak' ships (worst possible situation for them).
Fleet is reduced to 0 in an average of 36 'rounds'. (869 ships destroyed first round, out of 1000)
40 1 point guns, vs 50 '5 point soak' gives us an average of 195 rounds. (61 ships destroyed first round).
I think that's pretty conclusive, if not quite what I had expected. (And yes, I was assuming that ships can fully repair, since the 'model' involved a gun firing on a target for 30 seconds, and then 'switching', meaning it can retreat, and return when fully healed up. (there's ways to prevent them retreating, but I'm ignoring them because they also randomly target, and are applicable to both)
However I shall return to pondering the ponderableness of this problem, because I'm sure there's got to be a formula for it ;)