From 4b68761f4e70df055e849a1cb9c6095fea4bbf3b Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 4 May 2020 09:36:33 +0100 Subject: Starting on Challenge 2 this week. --- challenge-059/simon-proctor/raku/ch-2.raku | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenge-059/simon-proctor/raku/ch-2.raku (limited to 'challenge-059') diff --git a/challenge-059/simon-proctor/raku/ch-2.raku b/challenge-059/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..224906b720 --- /dev/null +++ b/challenge-059/simon-proctor/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +use v6; + +multi sub bit-diff( UInt $a, UInt $b where $a >= $b ) { + [+] ($a.base(2).comb.reverse Z (|$b.base(2).comb.reverse, |(lazy gather { take 0 }) ) ).map( { abs( $^a[0] - $^a[1] ) } ); +} + +multi sub bit-diff( UInt $a, UInt $b ) { bit-diff( $b, $a ) } + +#| Give the sum of the differences in bits between all the combinations of inputs +sub MAIN ( + *@vals where { $_.all ~~ UInt } #= Integer values to combine +){ + say [+] @vals.combinations(2).map( { bit-diff( |@_ ) } ); +} + -- cgit