Skip to content

Instantly share code, notes, and snippets.

@sorear
Created January 27, 2011 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorear/798221 to your computer and use it in GitHub Desktop.
Save sorear/798221 to your computer and use it in GitHub Desktop.
#!./perl6
# this is close to the Dynamic programming solution from Wikipedia,
my class knapsackItem{has $.name; has $.weight;has $.unit;}
my $MAX_WEIGHT = 400;
my @table;
push @table, knapsackItem.new: name => 'map' , weight => 9 , unit => 150;
push @table, knapsackItem.new: name => 'compass' , weight => 13 , unit => 35;
push @table, knapsackItem.new: name => 'water' , weight => 153 , unit => 200;
push @table, knapsackItem.new: name => 'sandwich' , weight => 50 , unit => 160;
push @table, knapsackItem.new: name => 'glucose' , weight => 15 , unit => 60;
push @table, knapsackItem.new: name => 'tin' , weight => 68 , unit => 45;
push @table, knapsackItem.new: name => 'banana' , weight => 27 , unit => 60;
push @table, knapsackItem.new: name => 'apple' , weight => 39 , unit => 40;
push @table, knapsackItem.new: name => 'cheese' , weight => 23 , unit => 30;
push @table, knapsackItem.new: name => 'beer' , weight => 52 , unit => 10;
push @table, knapsackItem.new: name => 'suntan cream' , weight => 11 , unit => 70;
push @table, knapsackItem.new: name => 'camera' , weight => 32 , unit => 30;
push @table, knapsackItem.new: name => 'T-shirt' , weight => 24 , unit => 15;
push @table, knapsackItem.new: name => 'trousers' , weight => 48 , unit => 10;
push @table, knapsackItem.new: name => 'umbrella' , weight => 73 , unit => 40;
push @table, knapsackItem.new: name => 'waterproof trousers' , weight => 42 , unit => 70;
push @table, knapsackItem.new: name => 'waterproof overclothes' , weight => 43 , unit => 75;
push @table, knapsackItem.new: name => 'note-case' , weight => 22 , unit => 80;
push @table, knapsackItem.new: name => 'sunglasses' , weight => 7 , unit => 20;
push @table, knapsackItem.new: name => 'towel' , weight => 18 , unit => 12;
push @table, knapsackItem.new: name => 'socks' , weight => 4 , unit => 50;
push @table, knapsackItem.new: name => 'book' , weight => 30 , unit => 10;
my @values;
my @weights;
my $maxi = +@table;
sub func ($i, $budget, $value, $how is rw) {
if $budget == 0 || $i == $maxi {
$how = 0;
$value;
} else {
my $how_p;
my $val_s = func($i + 1, $budget, $value, $how);
my $wt = @weights[$i];
if $budget >= $wt { # next one fits
my $val_p = func($i + 1, $budget - $wt, $value + @values[$i],
$how_p);
if $val_p > $val_s {
$how = $how_p +| (1 +< $i);
$val_p;
} else {
$val_s;
}
} else {
$val_s;
}
}
}
for @table -> $i {
push @values, $i.unit;
push @weights, $i.weight;
}
my $how;
my $value = func(0, $MAX_WEIGHT, 0, $how);
my @used;
for 0 ..^ +@table -> $ix {
push @used, @table[$ix] if $how +& (1 +< $ix);
}
say "Value = $value\nTourist put in the bag:\n{join " ", map *.name, @used}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment