Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Okay, the simplest approach here is to get all the combinations (via a convenient CPAN module.. Math::Combinatorics) and then filter those for the valid lances. There are almost certainly faster and more efficient ways to do this, but this seems to at least work.

A quick play with the module produced the following code, which at first glance seems to work:

use strict; use warnings; use Math::Combinatorics; my %mechs; while (<DATA>) { chomp; my @d = split /\s+/; $mechs{$d[0]} = { weight => $d[1], fraction => $d[2] }; } # find all lances with 5 members and 380t weight my @possible_lances = find_lances (3, 150); for my $lance (@possible_lances) { print "Found valid lance:\n"; my $lance_tonnage = 0; for my $mech (@$lance) { my $mech_weight = $mechs{$mech}->{weight}; print " $mech (${mech_weight}t)\n"; $lance_tonnage += $mech_weight; } print " Total tonnage: ${lance_tonnage}t\n"; } sub find_lances { my ($count, $max_tonnage) = @_; my @valid_lances; my $lance_combinations = Math::Combinatorics->new(count => $count, d +ata => [keys %mechs]); while (my @possible_lance = $lance_combinations->next_combination) { # Calculate the lance's tonnage my $tonnage = 0; $tonnage += $mechs{$_}->{weight} for @possible_lance; if ($tonnage <= $max_tonnage) { push @valid_lances, [@possible_lance]; } } 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 BlackHawk 50 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

In reply to Re: find combinations by Molt
in thread find combinations by holli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found