#! perl -slw use strict; sub genComb{ grep @$_, map{ my $ptn=$_; [ @_[ grep{ "${_}E0" if $ptn & (1<<$_) }0 .. $#_ ] ] } 0 .. 2**@_; } sub sum{ my $sum; $sum += $_ for @_; $sum } my %roster = map{ ("$_->[0]($_->[1])" => $_->[2]) } map{ chomp; [ split':' ] } ; my @possibles; unshift(@$_, sum @roster{@$_}) , push(@possibles, $_) for genComb keys %roster; printf '%3d will get you' . (' %s') x keys(%roster) . $/, @$_ for sort {$b->[0] <=> $a->[0]} @possibles; # To filter the list to that set costing less than $limit # substitute the following line for the line above # for grep{ $_ < $limit } 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