diff options
Diffstat (limited to 'challenge-324/alexander-karelas/perl/ch-2.pl')
| -rwxr-xr-x | challenge-324/alexander-karelas/perl/ch-2.pl | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-324/alexander-karelas/perl/ch-2.pl b/challenge-324/alexander-karelas/perl/ch-2.pl new file mode 100755 index 0000000000..7440e99035 --- /dev/null +++ b/challenge-324/alexander-karelas/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use v5.40; + +use Test2::V0; + +sub total_xor ($ints) { + my $max = 1; + $max *= 2 foreach @$ints; + my $total = 0; + for my $i (1 .. $max-1) { + my @bin = split //, sprintf('%0'. scalar(@$ints) .'b', $i); + my $xor = 0; + for (my $j = 0; $j < @bin; $j++) { + $xor = ($xor ^ $ints->[$j]) if $bin[$j]; + } + $total += $xor; + } + return $total; +} + +is total_xor([1, 3]), 6, 'Example 1'; +is total_xor([5, 1, 6]), 28, 'Example 2'; + +done_testing(); |
