aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/jaredor
diff options
context:
space:
mode:
authorJared Martin <jaredor+github@gmail.com>2020-05-07 09:17:03 -0500
committerJared Martin <jaredor+github@gmail.com>2020-05-07 09:17:03 -0500
commit8c07c3e93f48b59e6d8aa89414274b1d1cdc6d88 (patch)
tree6bd79c2ecb417d9e779aa98dd325c6ebe1365f04 /challenge-059/jaredor
parentf27afeb2622c5db9c2a6701c9f4435263af44369 (diff)
downloadperlweeklychallenge-club-8c07c3e93f48b59e6d8aa89414274b1d1cdc6d88.tar.gz
perlweeklychallenge-club-8c07c3e93f48b59e6d8aa89414274b1d1cdc6d88.tar.bz2
perlweeklychallenge-club-8c07c3e93f48b59e6d8aa89414274b1d1cdc6d88.zip
Pretty much the final version of Task #2 unless I want to handle huge integers
Diffstat (limited to 'challenge-059/jaredor')
-rwxr-xr-xchallenge-059/jaredor/perl/ch-2.pl28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-059/jaredor/perl/ch-2.pl b/challenge-059/jaredor/perl/ch-2.pl
index 5ba39a26a0..9acee3e679 100755
--- a/challenge-059/jaredor/perl/ch-2.pl
+++ b/challenge-059/jaredor/perl/ch-2.pl
@@ -1 +1,29 @@
#!/usr/bin/env perl
+
+use v5.012;
+use warnings;
+use Config;
+use List::Util qw(all sum);
+
+# 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}, 'N' );
+
+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 @nums = map { pack "${NN}*", $_ } @ARGV;
+
+my (@diffbits, $num);
+while ($num = shift @nums) {
+ push @diffbits, unpack( "%${LL}b*", $num ^ $_ ) for @nums;
+}
+say @diffbits ? sum @diffbits : 0;