aboutsummaryrefslogtreecommitdiff
path: root/challenge-192
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-11-22 18:15:29 +0000
committerGitHub <noreply@github.com>2022-11-22 18:15:29 +0000
commit2adbac29370f27a783346e7bcf778b053582bc77 (patch)
tree4b39edf6041be2bd5d229072a78aebda8e97fd85 /challenge-192
parent1259703cd5ff01f66e317af70e2399c912ca1cca (diff)
parent9357a325323ca1d242052520460b1ef82df2c064 (diff)
downloadperlweeklychallenge-club-2adbac29370f27a783346e7bcf778b053582bc77.tar.gz
perlweeklychallenge-club-2adbac29370f27a783346e7bcf778b053582bc77.tar.bz2
perlweeklychallenge-club-2adbac29370f27a783346e7bcf778b053582bc77.zip
Merge pull request #7145 from wlmb/challenges
Challenges
Diffstat (limited to 'challenge-192')
-rwxr-xr-xchallenge-192/wlmb/perl/ch-2a.pl27
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-192/wlmb/perl/ch-2a.pl b/challenge-192/wlmb/perl/ch-2a.pl
new file mode 100755
index 0000000000..73af1e7d78
--- /dev/null
+++ b/challenge-192/wlmb/perl/ch-2a.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 192
+# Task 2: Equal Distribution. Simpler version
+#
+# See https://wlmb.github.io/2022/11/21/PWC192/#task-2-equal-distribution
+use v5.36;
+use List::Util qw(sum all);
+die <<"EOF" unless @ARGV;
+Usage: $0 N1 [N2..]
+to count how many one-unit neighbor transfers are required to distribute the values
+in the integer array N1 N2... equally.
+EOF
+die "Only integers allowed" unless all {/[+-]?\d+/} @ARGV;
+my @arr=@ARGV;
+my $result=0;
+if((sum @arr)%@arr){
+ $result=-1
+} else {
+ $result=0;
+ my $avg=sum(@arr)/@arr;
+ for(1..@arr){
+ my $extra=$arr[$_-1]-$avg;
+ $arr[$_]+=$extra; # $arr[$_-1]-=$extra;
+ $result+=abs $extra;
+ }
+}
+say join " ", @ARGV, "->", $result;