aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/jaredor/perl/ch-2.pl
blob: 873484518ad8017589dcc88d99e9ece0ac8c3728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env perl

use v5.012;
use warnings;
use bigint;
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

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 $WORD = 2**$LL;

sub num2bitstr {
    my ( $numstr, $bitstr ) = ( $_[0], );
    $bitstr .= pack "${NN}", $numstr % $WORD and $numstr /= $WORD while $numstr;
    return $bitstr;
}

my @nums = map { num2bitstr $_ } @ARGV;

my ( @diffbits, $num );
while ( $num = pop @nums ) {
    push @diffbits, unpack( "%${LL}b*", $num ^ $_ ) for @nums;
}
say @diffbits ? sum @diffbits : 0;