diff options
| author | Flavio Poletti <flavio@polettix.it> | 2022-11-27 18:53:45 +0100 |
|---|---|---|
| committer | Flavio Poletti <flavio@polettix.it> | 2022-11-27 18:53:45 +0100 |
| commit | 9363b02cc50983bde06f6583522cd5e6922ce331 (patch) | |
| tree | 17f70871f0faf87da9d5a75534185b17b4d7c904 | |
| parent | e094f0679d0b4e8ee7a79c0045841209e964ff4c (diff) | |
| download | perlweeklychallenge-club-9363b02cc50983bde06f6583522cd5e6922ce331.tar.gz perlweeklychallenge-club-9363b02cc50983bde06f6583522cd5e6922ce331.tar.bz2 perlweeklychallenge-club-9363b02cc50983bde06f6583522cd5e6922ce331.zip | |
Add polettix's solution to challenge-192
| -rw-r--r-- | challenge-192/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-192/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-192/polettix/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-192/polettix/perl/ch-2.pl | 23 | ||||
| -rw-r--r-- | challenge-192/polettix/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-192/polettix/raku/ch-2.raku | 19 |
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; +} |
