aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2022-11-26 18:54:33 -0500
committerrir <rirans@comcast.net>2022-11-26 18:58:51 -0500
commitca88f58be93c6662d2f42e9a58b00793b459f472 (patch)
tree03a5725b727a5b1d5c2ca28a2c4369239d546b08
parentcdc9c25d5269fc208b75ef5807aa68723b11b665 (diff)
downloadperlweeklychallenge-club-ca88f58be93c6662d2f42e9a58b00793b459f472.tar.gz
perlweeklychallenge-club-ca88f58be93c6662d2f42e9a58b00793b459f472.tar.bz2
perlweeklychallenge-club-ca88f58be93c6662d2f42e9a58b00793b459f472.zip
192
-rwxr-xr-xchallenge-192/0rir/raku/ch-1.raku40
-rwxr-xr-xchallenge-192/0rir/raku/ch-2.raku103
2 files changed, 143 insertions, 0 deletions
diff --git a/challenge-192/0rir/raku/ch-1.raku b/challenge-192/0rir/raku/ch-1.raku
new file mode 100755
index 0000000000..d32dd3730f
--- /dev/null
+++ b/challenge-192/0rir/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤»
+use v6.d;
+use lib $?FILE.IO.parent(2).add("lib");
+use Test;
+
+=begin comment
+192-1: Binary Flip Submitted by: Mohammad S Anwar
+Given a positive integer, $n, find the binary flip; which is the bit-wise
+'not' of the bits expressing $n. For example 5 --> 101 --> 010 --> 2.
+
+Input: $n = 5
+Output: 2
+=end comment
+
+sub binary-flip( Any $n where * > 0 --> Int) {
+ $n.base(2).trans( <0 1> => <1 0>).parse-base(2).Int;
+}
+multi MAIN( Int $pos-integer ) {
+ say "Input: \$n = $pos-integer\n"
+ ~"Output: ", binary-flip($pos-integer);
+}
+multi MAIN ( 'test' ) {
+ my @Test = 32 => 31, 31 => 0, 30 => 1, 29 => 2, 28 => 3, 27 => 4,
+ 26 => 5, 25 => 6, 24 => 7, 23 => 8, 22 => 9, 21 => 10,
+ 20 => 11, 19 => 12, 18 => 13, 17 => 14, 16 => 15, 15 => 0,
+ 14 => 1, 13 => 2, 12 => 3, 11 => 4, 10 => 5, 9 => 6,
+ 8 => 7, 7 => 0, 6 => 1, 5 => 2, 4 => 3, 3 => 0,
+ 2 => 1,
+ 2⁶⁴ +1 => 2⁶⁴ -2, 2⁶⁴ => 2⁶⁴ -1,
+ 2⁶⁴ -8 => 7 , 2⁶⁴ -2 => 1 ,
+ 2¹⁰²⁴-7 => 6 , 2¹⁰²⁴-4 => 3 ,
+ 2¹⁰²⁴-2 => 1 ,
+ ;
+ for @Test -> $t {
+ is binary-flip($t.key), $t.value, "binary-flip($t.key()) --> $t.value()"
+ }
+ done-testing;
+}
+
diff --git a/challenge-192/0rir/raku/ch-2.raku b/challenge-192/0rir/raku/ch-2.raku
new file mode 100755
index 0000000000..b91e89f059
--- /dev/null
+++ b/challenge-192/0rir/raku/ch-2.raku
@@ -0,0 +1,103 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤»
+use v6.d;
+use lib $?FILE.IO.parent(2).add("lib");
+use Test;
+
+=begin comment
+192-2: Equal Distribution Submitted by: Mohammad S Anwar
+Given a list of integers greater than or equal to zero, @list, distribute the
+values so that all elements are same.
+If you succeed then print the total moves otherwise print -1.
+
+Please follow the rules (as suggested by Neils van Dijke [2022-11-21 13:00]
+
+1) You can only move a value of '1' per move
+2) You are only allowed to move a value of '1' to a direct neighbor/adjacent cell
+
+Example 1:
+Input: @list = (1, 0, 5)
+Output: 4
+
+Example 2: Input: @list = (0, 2, 0) Output: -1
+Example 3: Input: @list = (0, 3, 0) Output: 2
+=end comment
+
+=comment Sorry: I am not tapping each circle as I go round the board.
+
+class Equalizer {
+ has @.people is rw;
+ has Pair $!rich;
+ has Pair $!poor;
+ has Int $!mean;
+ has Int $!h = 0;
+ has Int $!t;
+ has Int $!effort = 0;
+
+ submethod TWEAK {
+ die 'Got Empty array.' if @!people ~~ Empty;
+ $!t = @!people.elems;
+ my $ave = ( ([+] @!people) ÷ $!t-- );
+ $!mean = $ave.Int == $ave ?? $ave.Int !! Int ;
+ }
+ method swap( ) {
+ my $need = $!mean - $!poor.value;
+ my $surplus = $!rich.value - $!mean;
+ my $amt = ($need, $surplus).min;
+ $!rich.value -= $amt;
+ $!poor.value += $amt;
+ $!effort += $amt × abs( $!rich.key - $!poor.key);
+ }
+ method redistribution( --> Int) {
+ return Int if not defined $!mean;
+ return 0 if $!mean == 0;
+ loop {
+ $!rich = @.people[$!h..$!t].first( * > $!mean, :kv).pairup[0].Pair;
+ $!poor = @.people[$!h..$!t].first( * < $!mean, :kv).pairup[0].Pair;
+ last unless $!rich.defined and $!poor.defined;
+ $!h = ($!rich.key, $!poor.key).min;
+ self.swap;
+ }
+ $!effort;
+ }
+}
+
+multi MAIN ( @list = [ 1, 0, 5] ) {
+ my $c = Equalizer.new( people => @list );
+ $c = $c.redistribution;
+ $c = $c.defined ?? $c !! Int;
+ say "Input: \@list = @list.raku()\nOutput: $c.raku()";
+}
+
+multi MAIN ( 'test' ) {
+ my @Test =
+ { in => [ 1, 0, 5], exp => 4, },
+ { in => [ 0, 2, 0], exp => Int, },
+ { in => [ 0, 3, 0], exp => 2, },
+ { in => [ 0,], exp => 0, },
+ { in => [ 1000,], exp => 0, },
+ { in => [ 2,2], exp => 0, },
+ { in => [2,2,2], exp => 0, },
+ { in => [0,0,2,], exp => Int, },
+ { in => [3, 0, 3], exp => 2, },
+ { in => [1,2,3,4,5], exp => '10',},
+ { in => [1,2,8,4,5], exp => '10',},
+ { in => [1,2,7,4,5], exp => Int, },
+ { in => [11,1,1,1, 1,1,1,1,1, 1], exp => '45', },
+ { in => [ 1,1,1,1, 1,1,1,1,1,11], exp => '45', },
+ { in => [ 1,1,1,1,11,1,1,1,1, 1], exp => '25', },
+ { in => [ 6,1,1,1, 1,1,1,1,1, 6], exp => '20', },
+ { in => [ 5,1,1,1, 3,1, 1,1,1, 5], exp => '13', },
+ { in => [ 1,1,1,3,1,10,1,1,1,1,1], exp => '26', },
+ { in => [ 3,0,0,5,0,0,0,0,0,0,3,5,0,0,0,0,0,0,0,0,0,6], exp => '41', },
+ ;
+
+ plan +@Test;
+
+ for @Test -> %t {
+ my $e = Equalizer.new: people => @(%t<in>.Array);
+ is $e.redistribution(), %t<exp>, "%t<in> --> %t<exp>.Int.raku()";
+ }
+ done-testing;
+ exit;
+}