aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/javier-luque/raku/ch-2.p6
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-059/javier-luque/raku/ch-2.p6')
-rw-r--r--challenge-059/javier-luque/raku/ch-2.p626
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-059/javier-luque/raku/ch-2.p6 b/challenge-059/javier-luque/raku/ch-2.p6
new file mode 100644
index 0000000000..707105dd9a
--- /dev/null
+++ b/challenge-059/javier-luque/raku/ch-2.p6
@@ -0,0 +1,26 @@
+# Test: perl6 ch-2.p6 2 3 4
+sub MAIN(*@ARGV) {
+ my @combos = @ARGV.combinations: 2;
+ my $answer = 0;
+
+ for @combos -> $combo {
+ $answer += f($combo[0], $combo[1]);
+ }
+
+ say $answer;
+}
+
+sub f(Int $a, Int $b) {
+ return calculate-true-bits($a +^ $b);
+}
+
+# Calculate the number of true bits
+sub calculate-true-bits(Int $n is copy) {
+ my $count = 0;
+
+ repeat {
+ $count++ if ($n +& 1);
+ } while ($n = $n +> 1);
+
+ return $count;
+}