#! /usr/bin/env raku unit sub MAIN (:$verbose); my %weight = (R => 1, B => 1, G => 2, Y => 12, P => 4); my %value = (R => 1, B => 2, G => 2, Y => 4, P => 10); constant $maxweight = 15; my @boxes = %weight.keys.sort; say @boxes.combinations if $verbose; # The first list is empty, so get rid of that. my %w; my %v; for @boxes.combinations.grep(*.elems) -> @list { my $weight = @list.map({ %weight{$_} }).sum; my $value = @list.map({ %value{$_} }).sum; my $key = @list.join; if $weight <= $maxweight { %w{$key} = $weight; %v{$key} = $value; say "{ @list } -> $weight kg -> £ $value" if $verbose; } elsif $verbose { say "{ @list } -> $weight kg -> £ $value (> $maxweight kg; ignored)"; } } my $max = %v.values.max; say "Highest value: £ $max" if $verbose; my @solutions = %w.keys.grep( { %v{$_} == $max } ); my $min = @solutions.map( { %w{$_} } ).min; say "Lowest weight: $min kg" if $verbose; for @solutions -> $solution { say "{ $solution.comb.join(",") }: { %w{$solution} } kg at £ { %v{$solution} }." if %w{$solution} == $min; }