aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-28 00:55:55 +0000
committerGitHub <noreply@github.com>2022-11-28 00:55:55 +0000
commitbfcea82a24a5b783135720f1ac132b0951228a99 (patch)
tree5cdcdeb98861faeba7c259b3ebe7c21d760a957b
parent0934f5c4ce425a81fc3d5477b8ef6b695f0394dc (diff)
parent9363b02cc50983bde06f6583522cd5e6922ce331 (diff)
downloadperlweeklychallenge-club-bfcea82a24a5b783135720f1ac132b0951228a99.tar.gz
perlweeklychallenge-club-bfcea82a24a5b783135720f1ac132b0951228a99.tar.bz2
perlweeklychallenge-club-bfcea82a24a5b783135720f1ac132b0951228a99.zip
Merge pull request #7164 from polettix/polettix/pwc192
Add polettix's solution to challenge-192
-rw-r--r--challenge-192/polettix/blog.txt1
-rw-r--r--challenge-192/polettix/blog1.txt1
-rw-r--r--challenge-192/polettix/perl/ch-1.pl18
-rw-r--r--challenge-192/polettix/perl/ch-2.pl23
-rw-r--r--challenge-192/polettix/raku/ch-1.raku14
-rw-r--r--challenge-192/polettix/raku/ch-2.raku19
6 files changed, 76 insertions, 0 deletions
diff --git a/challenge-192/polettix/blog.txt b/challenge-192/polettix/blog.txt
new file mode 100644
index 0000000000..f8c144d9b5
--- /dev/null
+++ b/challenge-192/polettix/blog.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2022/11/24/pwc192-binary-flip/
diff --git a/challenge-192/polettix/blog1.txt b/challenge-192/polettix/blog1.txt
new file mode 100644
index 0000000000..74ccc6101b
--- /dev/null
+++ b/challenge-192/polettix/blog1.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2022/11/25/pwc192-equal-distribution/
diff --git a/challenge-192/polettix/perl/ch-1.pl b/challenge-192/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..628640fbcc
--- /dev/null
+++ b/challenge-192/polettix/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+say binary_flip(shift // 5);
+
+sub binary_flip ($n) {
+ my $mask = 0x01;
+ my $result = 0;
+ while ($n) {
+ $result |= $mask unless $n & 0x01;
+ $n >>= 1;
+ $mask <<= 1;
+ }
+ return $result;
+}
diff --git a/challenge-192/polettix/perl/ch-2.pl b/challenge-192/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..9a05a26ec0
--- /dev/null
+++ b/challenge-192/polettix/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util 'sum';
+
+my @inputs = map { split m{[\s,]+}mxs } @ARGV;
+@inputs = qw< 1 0 5 > unless @inputs;
+
+say equal_distribution(@inputs);
+
+sub equal_distribution (@inputs) {
+ my $total = sum(@inputs);
+ return -1 if $total % @inputs;
+ my $average = $total / @inputs;
+ my ($delta, $moves) = (0, 0);
+ for my $value (@inputs) {
+ $moves += abs($delta);
+ $delta += $value - $average;
+ }
+ return $moves;
+}
diff --git a/challenge-192/polettix/raku/ch-1.raku b/challenge-192/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..e8671fd9b3
--- /dev/null
+++ b/challenge-192/polettix/raku/ch-1.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN ($n = 5) { put binary-flip($n) }
+
+sub binary-flip (Int $n is copy where * > 0) {
+ my $mask = 0x01;
+ my $result = 0;
+ while $n {
+ $result +|= $mask unless $n +& 1;
+ $n +>= 1;
+ $mask +<= 1;
+ }
+ return $result;
+}
diff --git a/challenge-192/polettix/raku/ch-2.raku b/challenge-192/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..3958172da0
--- /dev/null
+++ b/challenge-192/polettix/raku/ch-2.raku
@@ -0,0 +1,19 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (*@args) {
+ my @inputs = @argsĀ».split(/<[ \s , ]>+/).Slip.flat;
+ @inputs = <1 0 5> unless @inputs;
+ put equal-distribution(@inputs);
+}
+
+sub equal-distribution (@inputs) {
+ my $total = @inputs.sum;
+ return -1 unless $total %% @inputs;
+ my $average = ($total / @inputs).Int;
+ my ($delta, $moves) = 0, 0;
+ for @inputs -> $value {
+ $moves += $delta.abs;
+ $delta += $value - $average;
+ }
+ return $moves;
+}