From 8c07c3e93f48b59e6d8aa89414274b1d1cdc6d88 Mon Sep 17 00:00:00 2001 From: Jared Martin Date: Thu, 7 May 2020 09:17:03 -0500 Subject: Pretty much the final version of Task #2 unless I want to handle huge integers --- challenge-059/jaredor/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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; -- cgit