aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-192/LoneWolfiNTj/perl/ch-1.pl91
-rwxr-xr-xchallenge-192/LoneWolfiNTj/perl/ch-2.pl140
2 files changed, 231 insertions, 0 deletions
diff --git a/challenge-192/LoneWolfiNTj/perl/ch-1.pl b/challenge-192/LoneWolfiNTj/perl/ch-1.pl
new file mode 100755
index 0000000000..1fafba4cb2
--- /dev/null
+++ b/challenge-192/LoneWolfiNTj/perl/ch-1.pl
@@ -0,0 +1,91 @@
+#! /usr/bin/env perl
+# PWCC Challenge 192 Task 1 solution by Robbie Hatley
+
+=pod
+
+Task 1: Binary Flip
+Submitted by: Mohammad S Anwar
+
+You are given a positive integer, $n. Write a script to find the
+binary flip (that is, the positive integer consisting of all of
+the significant digits of $n inverted).
+
+Example 1: Input=5 Output=2
+Example 2: Input=4 Output=3
+Example 3: Input=6 Output=1
+
+=cut
+
+# NOTE: Input is by either default array or CLI. If inputting
+# by CLI, input must be a space-separated sequence of
+# one-or-more positive integers.
+
+# NOTE: Output is to stdout and will be the binary flip(s)
+# of the input number(s).
+
+use v5.36;
+
+# Does a string represent a positive integer?
+sub is_pos_int ($x)
+{
+ if ($x =~ m/^[1-9][0-9]*$/) {return 1}
+ else {return 0}
+}
+
+# Do ALL strings in an array prepresent positive integers?
+sub all_are_pos_int (@a)
+{
+ for (@a) {if (!is_pos_int($_)) {return 0}}
+ return 1;
+}
+
+# What is the number of binary significant figures in a positive integer?
+sub sigfigs ($n)
+{
+ my $sigfigs = 0;
+ while ($n)
+ {
+ ++$sigfigs;
+ $n >>= 1;
+ }
+ return $sigfigs;
+}
+
+# Default input:
+my @array = (5,4,6);
+
+# Non-default input:
+if (scalar(@ARGV) > 0)
+{
+ if (all_are_pos_int(@ARGV))
+ {
+ @array = @ARGV;
+ }
+ else
+ {
+ die "Error: input must be space-separated sequence of ".
+ "one-or-more positive integers.\n";
+ }
+}
+
+# Process inputs and print outputs:
+for (@array)
+{
+ my $n = $_ ;
+
+ # How many sig figs are we dealing with here?
+ my $sigfigs = &sigfigs($n);
+
+ # Get the raw inverse of $n, including the left-zero-padding bits:
+ my $inverse = ~$n;
+
+ # Get the flip:
+ my $flip = 0;
+ for ( my $idx = 0 ; $idx < $sigfigs ; ++$idx )
+ {
+ $flip += ((($inverse >> $idx) % 2) << $idx);
+ }
+ say "$n flipped = $flip";
+}
+exit;
+__END__ \ No newline at end of file
diff --git a/challenge-192/LoneWolfiNTj/perl/ch-2.pl b/challenge-192/LoneWolfiNTj/perl/ch-2.pl
new file mode 100755
index 0000000000..eeefb2932e
--- /dev/null
+++ b/challenge-192/LoneWolfiNTj/perl/ch-2.pl
@@ -0,0 +1,140 @@
+#! /usr/bin/env perl
+# PWCC Challenge 192 Task 1 solution by Robbie Hatley
+
+=pod
+
+Task 2: Communism (Equal Distribution)
+Submitted by: Mohammad S Anwar
+
+Given a list of non-negative integers, write a script to distribute
+the total value of the numbers in the list evenly among the list's
+members so that every member is the same number (if that's possible).
+If you succeed then print the total moves; otherwise, print -1.
+Please follow the rules suggested by Neils van Dijke:
+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=(1, 0, 5) Output=4
+Example 2: Input=(0, 2, 0) Output=-1
+Example 3: Input=(0, 3, 0) Output=2
+
+=cut
+
+# NOTE: Input is by either default array-of-arrays, or by CLI.
+# If inputting by CLI, input must be a single space-separated
+# sequence of one or more integers.
+
+# NOTE: Output is to stdout and will be either number of steps to
+# achieve equal wealth distribution among citizens, or -1 if
+# no such distribution is possible.
+
+use v5.36;
+use List::Util "sum0";
+
+# Does a string represent an integer?
+sub is_int ($x)
+{
+ if ($x =~ m/(?:^0$)|(?:^-?[1-9][0-9]*$)/) {return 1}
+ else {return 0}
+}
+
+# Do ALL strings in an array prepresent integers?
+sub all_are_int (@a)
+{
+ for (@a) {if (!is_int($_)) {return 0}}
+ return 1;
+}
+
+# Is the wealth distribution of the citizens of this realm equal?
+sub egalite (@array)
+{
+ for (1..$#array) {if ($array[$_]!=$array[0]) {return 0}}
+ return 1
+}
+
+# Is the wealth in this realm NOT evenly distributable?
+sub noblesse_oblige (@array)
+{
+ if (sum0(@array)%scalar(@array)) {return 1}
+ return 0
+}
+
+# What is the SSN of the left-most poorest citizen?
+sub lmp (@array)
+{
+ my $lmp_idx = 0;
+ my $lmp_amt = $array[0];
+ for (1..$#array) {if ($array[$_] < $lmp_amt) {$lmp_idx = $_;$lmp_amt=$array[$_]}}
+ return $lmp_idx;
+}
+
+# What is the SSN of the left-most richest citizen?
+sub lmr (@array)
+{
+ my $lmr_idx = 0;
+ my $lmr_amt = $array[0];
+ for (1..$#array) {if ($array[$_] > $lmr_amt) {$lmr_idx = $_;$lmr_amt=$array[$_]}}
+ return $lmr_idx;
+}
+
+# Default inputs:
+my @inputs = ([1,0,5],[0,2,0],[0,3,0]);
+
+# Non-default input:
+if (scalar(@ARGV) > 0)
+{
+ if (all_are_int(@ARGV))
+ {
+ @inputs = ([@ARGV]);
+ }
+ else
+ {
+ die "Error: input must be space-separated sequence of ".
+ "one-or-more integers.\n";
+ }
+}
+
+# Process each input array:
+for (@inputs)
+{
+ my @array = @$_;
+ my @copy = @array;
+ my $moves;
+
+ # Do the citizens of this realm already possess the same # of $ each?
+ if (egalite(@array))
+ {$moves = 0;}
+
+ # Is the wealth in this realm NOT evenly distributable?
+ elsif (noblesse_oblige(@array))
+ {$moves = -1;}
+
+ # If we get to here, we have wealth to redistribute! Eat the rich!
+ else
+ {
+ $moves = 0;
+ while (!egalite(@copy))
+ {
+ my $lmp=lmp(@copy);
+ my $lmr=lmr(@copy);
+ my $vec=$lmp-$lmr;
+ my $dir=$vec/abs($vec);
+ my $idx=$lmr;
+ while ($idx!=$lmp)
+ {
+ my $nxt = $idx + $dir;
+ --$copy[$idx];
+ ++$copy[$nxt];
+ ++$moves;
+ $idx=$nxt;
+ } # end while (moving $1)
+ } # end while (no egalite yet)
+ } # end else (eat the rich)
+
+ # Announce moves for this input array:
+ say "(@array) -> $moves";
+
+} # end for each input array
+exit; # end program
+__END__ # end file \ No newline at end of file