From a834949c074305ec3092514516db68ea9e3e416c Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 21 Nov 2022 10:05:18 +0000 Subject: Challenges done :) --- challenge-192/simon-proctor/raku/ch-1.raku | 21 ++++++++++++++ challenge-192/simon-proctor/raku/ch-2.raku | 44 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 challenge-192/simon-proctor/raku/ch-1.raku create mode 100644 challenge-192/simon-proctor/raku/ch-2.raku diff --git a/challenge-192/simon-proctor/raku/ch-1.raku b/challenge-192/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..1be51bfbc4 --- /dev/null +++ b/challenge-192/simon-proctor/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + +multi sub MAIN("TEST") is hidden-from-USAGE { + use Test; + is bit-flip(5), 2; + is bit-flip(4), 3; + is bit-flip(6), 1; + is bit-flip(2), 1; + is bit-flip(3), 0; + is bit-flip(1), 0; + done-testing; +} + +#| Given an Int $x find the binary flip of it +multi sub MAIN( IntStr $x ) { + bit-flip($x).say; +} + +sub bit-flip( Int() $x ) returns Int { + $x.base(2).comb().map( { abs($_-1) } ).join("").parse-base(2); +} diff --git a/challenge-192/simon-proctor/raku/ch-2.raku b/challenge-192/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..479deba939 --- /dev/null +++ b/challenge-192/simon-proctor/raku/ch-2.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku + +multi sub MAIN("TEST") is hidden-from-USAGE { + use Test; + is flatten-moves( [1,0,5] ), 4; + is flatten-moves( [0,2,0] ), -1; + is flatten-moves( [0,3,0] ), 2; + done-testing; +} + +multi sub MAIN( *@vals ) { + flatten-moves( @vals.List ).say; +} + +subset CanAverage of Array where -> @a { my $avg = ( ([+] @a) / @a.elems ); $avg ~~ $avg.Int }; + + +multi sub flatten-moves( $ ) { -1 } + +multi sub flatten-moves( CanAverage $v ) { + my $moves = 0; + my $avg = ( ([+] $v) / $v.elems ); + my $point = 0; + while ( (all( $v ) !~~ $avg) ) { + if ( $v[$point] ~~ $avg ) { + $point++; + next; + } + if ( $v[$point] > $avg ) { + $v[$point]--; + $v[$point+1]++; + $moves++; + next; + } + if ( $v[$point] < $avg ) { + $v[$point]++; + $v[$point+1]--; + $moves++; + next; + } + + } + return $moves; +} -- cgit