use strict; use warnings; use Math::Combinatorics; use Algorithm::Permute; use Data::Dumper; my %mechs; while () { chomp; my ($mech, $weight, $faction) = split /\s+/; $mechs{$mech} = { weight => $weight, faction => $faction }; } # find all lances with 3 members and 150t weight my @possible_lances = find_lances ( 3, 150, 10, grep { $mechs{$_}->{faction} eq "clan" } keys %mechs ); for my $lance ( @possible_lances ) { print join (",", @{$lance->{names}}), "(", $lance->{weight}, " tons)\n"; } sub find_lances { my $players = shift; my $weight = shift; my $treshold = shift; my @all_lances = Math::Combinatorics::combine ($players, @_); my @valid_lances; for my $lance ( @all_lances ) { my $sum; $sum += $mechs{$_}->{weight} for @{$lance}; push @valid_lances, { names=>$lance, weight=>$sum } if ($weight - $sum <= $treshold) && ($weight - $sum) >= 0 ; } return @valid_lances; } __DATA__ Flea 20 clan Commando 25 clan UrbanMech 30 clan Hollander 35 clan Jenner 35 clan Raven 35 clan Wolfhound 35 clan Black 50 Hawk clan Hunchback 50 clan Rifleman 60 clan Catapult 65 clan Loki 65 clan Thor 70 clan MadCat 75 clan Gargoyle 80 clan Victor 80 clan Zeus 80 clan Longbow 85 clan Warhawk 85 clan Mauler 90 clan Atlas 100 clan