use strict; use warnings; use re 'eval'; use List::Util 'sum'; my $max_weight = 400; my %items = ( # value, weight: positive integers 'map' => { v => 150, w => 9 }, 'compass' => { v => 35, w => 13 }, 'water' => { v => 200, w => 153 }, 'sandwich' => { v => 160, w => 50 }, 'glucose' => { v => 60, w => 15 }, 'tin' => { v => 45, w => 68 }, 'banana' => { v => 60, w => 27 }, 'apple' => { v => 40, w => 39 }, 'cheese' => { v => 30, w => 23 }, 'beer' => { v => 10, w => 52 }, 'suntan_cream' => { v => 70, w => 11 }, 'camera' => { v => 30, w => 32 }, 't_shirt' => { v => 15, w => 24 }, 'trousers' => { v => 10, w => 48 }, 'umbrella' => { v => 40, w => 73 }, 'waterproof_trousers' => { v => 70, w => 42 }, 'waterproof_overclothes' => { v => 75, w => 43 }, 'note_case' => { v => 80, w => 22 }, 'sunglasses' => { v => 20, w => 7 }, 'towel' => { v => 12, w => 18 }, 'socks' => { v => 50, w => 4 }, 'book' => { v => 10, w => 30 }, ); my $str = 'v' x sum( map $_->{v}, values %items ) . '0' . 'w' x $max_w +eight; #print $str, "\n"; my $i; my $left = my $right = ''; for ( keys %items ) { $left .= sprintf "(?<%s>(?:%s)?)\n", $_, 'v' x $items{$_}{v}; $right .= sprintf "(?(?{ \$%d })%s|)\n", ++$i, 'w' x $items{$_}{w}; } my $re = sprintf "%s0\n(?=\n%s)\n", $left, $right; #print $re; my $sum = join '+', map length, grep length, $str =~ /$re/x; print $sum, '=', eval $sum, "\n"; print join "\n", grep { length $+{$_} } keys %+; print "\n"; __END__
The input data came from rosettacode.org. With this input it runs for circa five minutes on my machine. If you want to play with it, just replace the input with something simpler, for instance:
# a partition problem as a special case my $max_weight = 136; my %items = ( a => { w => 9, v => 9 }, b => { w => 12, v => 12 }, c => { w => 14, v => 14 }, d => { w => 17, v => 17 }, e => { w => 23, v => 23 }, f => { w => 32, v => 32 }, g => { w => 34, v => 34 }, h => { w => 40, v => 40 }, i => { w => 42, v => 42 }, j => { w => 49, v => 49 }, );
To understand what it does, just uncomment the print lines, supply it with some simple input, and see the contents of $str and $re for yourself. Probably you also want to replace the named captures with standard capturing parentheses in the $left .= ... line as the naming is not essential, they're just there for the pretty printing of the output.
I'm still not fully satisfied with this solution, because the regex is not pure. (By saying pure I do not mean the compsci sense, but that it does not contain embedded code.) Anyway it's close to it, as the only non-pure part of it is something like this: (?(?{ $1 })ww|). But I wasn't able to use (?(1)ww|) instead, because my captures are always matching, though sometimes matching the empty string.
For the educated monks there's probably nothing new in this since much of my inspiration came from Abigail's previous works:
- the prime tester regex
- Backtracking through the regex world
- The N-queens problem using pure regexes
- Finding Hamiltonian Paths using the Regexp Engine
- Pure regex Hamiltonian Circuit solution
- Are Perl patterns universal?
- related theory on MJD's site
If you've found bugs in the code above, please let me know.
Cheers,
rubasov
p.s. I've hesitated to post it to the Cool Uses for Perl section instead of Meditations, but all the similar posts were in Meditations so I posted here. Feel free to move it, if it fits better in the CUfP section.
|
---|