Created
June 4, 2012 20:29
-
-
Save rbucker/2870673 to your computer and use it in GitHub Desktop.
slightly refactored version of the same code less side effects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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