aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorJared Martin <jaredor+github@gmail.com>2020-05-09 16:59:52 -0500
committerJared Martin <jaredor+github@gmail.com>2020-05-09 16:59:52 -0500
commit545b1ac0f487c742b81ee0772cae1f3de36978d1 (patch)
tree5c89a7d02d329c5764dd485232c8f2b2dccfde77 /challenge-059
parent177df50b7a6f4702e666b5e7377a8ffe4eef61f1 (diff)
downloadperlweeklychallenge-club-545b1ac0f487c742b81ee0772cae1f3de36978d1.tar.gz
perlweeklychallenge-club-545b1ac0f487c742b81ee0772cae1f3de36978d1.tar.bz2
perlweeklychallenge-club-545b1ac0f487c742b81ee0772cae1f3de36978d1.zip
use bigint and chop arbitrarilly large numbers into words to be bitwise diffed.
Diffstat (limited to 'challenge-059')
-rwxr-xr-xchallenge-059/jaredor/perl/ch-2.pl38
1 files changed, 29 insertions, 9 deletions
diff --git a/challenge-059/jaredor/perl/ch-2.pl b/challenge-059/jaredor/perl/ch-2.pl
index ce5ae1f0cd..4cd8a0563b 100755
--- a/challenge-059/jaredor/perl/ch-2.pl
+++ b/challenge-059/jaredor/perl/ch-2.pl
@@ -2,28 +2,48 @@
use v5.012;
use warnings;
+use bigint;
use Config;
-use List::Util qw(all sum);
+use List::Util qw(all sum max);
# PWC 059, TASK #2 : Bit Sum
# Answer based on perl doc for unpack and www.perlmonks.org/?node_id=407933
+my ( $LL, $NN ) =
+ defined $Config{longlongsize}
+ ? ( 8 * $Config{longlongsize}, 'Q' )
+ : ( 8 * $Config{longsize}, 'L' );
+
+my $CHOP_SIZE = 2**${LL};
+my $CHOP_ZERO = pack "${NN}", 0;
+
die "This script requires one or more positive integer arguments."
unless @ARGV;
die "Not all arguments to the script are positive integers."
unless all { /\A [1-9] \d* \Z/xms } @ARGV;
-my ( $LL, $NN ) =
- defined $Config{longlongsize}
- ? ( 8 * $Config{longlongsize}, 'Q' )
- : ( 8 * $Config{longsize}, 'L' );
+#my @nums = map { pack "${NN}*", $_ } @ARGV;
+
+sub chop_up {
+ my ( $num, @chopped ) = ( 0 + $_[0], );
+ while ($num) {
+ push @chopped, $num % $CHOP_SIZE;
+ $num = int( $num / $CHOP_SIZE );
+ }
+ return [ map { pack "${NN}", $_ } @chopped ];
+}
-my @nums = map { pack "${NN}*", $_ } @ARGV;
+my @nums = map { chop_up $_ } @ARGV;
+my $chops = max map { $#$_ } @nums;
+( $chops - $#$_ ) and push @$_, ($CHOP_ZERO) x ( $chops - $#$_ ) for @nums;
-my ( @diffbits, $num );
-while ( $num = pop @nums ) {
- push @diffbits, unpack( "%${LL}b*", $num ^ $_ ) for @nums;
+my ( @diffbits, $numa );
+while ( $numa = shift @nums ) {
+ for my $numb (@nums) {
+ push @diffbits, unpack( "%${LL}b*", $numa->[$_] ^ $numb->[$_] )
+ for 0 .. $chops;
+ }
}
say @diffbits ? sum @diffbits : 0;