aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/jaredor/perl/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-059/jaredor/perl/ch-2.pl')
-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;