Skip to content

Instantly share code, notes, and snippets.

@rbucker rbucker/combo2.pl
Created Jun 4, 2012

Embed
What would you like to do?
slightly refactored version of the same code less side effects.
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Math::BigInt;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use List::MoreUtils qw( pairwise );
my $t = 10;
my %d = ("apple"=>3, "pear"=>2, "pineapple"=>9, "strawberry"=>1,
"kiwi"=>-5, "lime"=>15, "lemon"=>5, "orange"=>-1, "banana"=>5);
my $n = scalar(keys %d);
my $l = sprintf("%d", $n);
my $x = Math::BigInt->new("2");
$x->bpow(sprintf("%d", $n));
$x->bdec();
sub xdump($) {
my $arr = shift;
my $p;
print "[";
foreach $p (@$arr) {
print $p."(".$d{$p}."), ";
}
print "]\n";
}
while( $x->is_pos() ) {
my @by = split(//,substr('0' x $l . substr($x->as_bin(), 2),-1*$l));
my @k = keys %d;
my @z = pairwise {[$a,$b]} @k, @by;
my $k = (reduce { if(@$b[1] eq '1') {push($a,@$b[0]);$a} else {$a}} [], @z)[0];
my $sum = reduce { $a + $d{$b} } 0, @$k;
if( $sum == $t ) {
print $x.". .".$x->as_hex()." ";
xdump($k);
}
$x->bdec();
}
# __END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.