#! perl -slw use strict; sub genComb{ # Generate all the combos of the input list grep @$_, map{ my $ptn=$_; [ @_[ grep{ "${_}E0" if $ptn & (1<<$_) }0 .. $#_ ] ] } 0 .. 2**@_; } sub sum{ my $sum; $sum += $_ for @_; $sum } # Sum an array # Build the roster from the my %roster = map{ ("$_->[0]($_->[1])" => $_->[2]) } map{ chomp; [ split':' ] } ; my @possibles; unshift(@$_, sum @roster{@$_}) # Add the total cost to the front of the array , push(@possibles, $_) # and save it for genComb keys %roster; # for all possible combinations # Print the combos out reverse sorted by cost. printf '%3d will get you' . (' %s') x keys(%roster) . $/, @$_ for sort {$b->[0] <=> $a->[0]} @possibles; __DATA__ blastaar:v:138 mr fixit:u:104 daredevil:r:30 blob:v:51 boomerang:e:34 wolverine:e:61 skrull commando:v:18