diff options
| author | Scimon <simon.proctor@gmail.com> | 2022-11-21 10:05:18 +0000 |
|---|---|---|
| committer | Scimon <simon.proctor@gmail.com> | 2022-11-21 10:05:18 +0000 |
| commit | a834949c074305ec3092514516db68ea9e3e416c (patch) | |
| tree | 222a0262650a716b77aafd40eaf5f727cdcc319e | |
| parent | 8ea75399a70f8ab625f105a9d5ca011759e9dc19 (diff) | |
| download | perlweeklychallenge-club-a834949c074305ec3092514516db68ea9e3e416c.tar.gz perlweeklychallenge-club-a834949c074305ec3092514516db68ea9e3e416c.tar.bz2 perlweeklychallenge-club-a834949c074305ec3092514516db68ea9e3e416c.zip | |
Challenges done :)
| -rw-r--r-- | challenge-192/simon-proctor/raku/ch-1.raku | 21 | ||||
| -rw-r--r-- | challenge-192/simon-proctor/raku/ch-2.raku | 44 |
2 files changed, 65 insertions, 0 deletions
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; +} |
