Skip to content

Instantly share code, notes, and snippets.

@rbucker
Created June 5, 2012 16:32
Show Gist options
  • Save rbucker/2876097 to your computer and use it in GitHub Desktop.
Save rbucker/2876097 to your computer and use it in GitHub Desktop.
remove my xdump() and replace with Data::Dumper
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Bit::Vector::Overload;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use List::MoreUtils qw( pairwise );
my $target_sum = 10;
my %data = (apple=>3, pear=>2, pineapple=>9, strawberry=>1,
kiwi=>-5, lime=>15, lemon=>5, orange=>-1, banana=>5);
my @keys = keys %data;
my $n = @keys;
my $vector = Bit::Vector::Overload->new($n);
$vector->Configuration("output=bin");
$vector->Fill();
print Dumper(\%data);
$Data::Dumper::Indent = 0;
my $carry = 0;
while( ! $carry ) {
my @bits = split(//, "$vector");
my @pairs= pairwise { [$a,$b] } @keys, @bits;
my $kset = (reduce { push($a,@$b[0]) if(int(@$b[1])); $a} [], @pairs)[0];
my $sum = reduce { $a + $data{$b} } 0, @$kset;
if( $sum == $target_sum ) {
print $vector->to_Hex().": ".Dumper($kset) ."\n";
}
$carry = $vector->decrement();
}
# __END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment