aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-21 14:05:19 +0000
committerGitHub <noreply@github.com>2022-11-21 14:05:19 +0000
commit1e9718196dbd0bbb4ec46af3b55598f782fb5458 (patch)
tree83d3c00b445c2c5cea2ad64351809c27be3fa512
parent86e5aca5600ebeda34e424eadff0e0d1803aa45c (diff)
parenta834949c074305ec3092514516db68ea9e3e416c (diff)
downloadperlweeklychallenge-club-1e9718196dbd0bbb4ec46af3b55598f782fb5458.tar.gz
perlweeklychallenge-club-1e9718196dbd0bbb4ec46af3b55598f782fb5458.tar.bz2
perlweeklychallenge-club-1e9718196dbd0bbb4ec46af3b55598f782fb5458.zip
Merge pull request #7128 from Scimon/master
Challenges done :)
-rw-r--r--challenge-192/simon-proctor/raku/ch-1.raku21
-rw-r--r--challenge-192/simon-proctor/raku/ch-2.raku44
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;
+}