aboutsummaryrefslogtreecommitdiff
path: root/challenge-080
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2020-10-03 17:08:44 +0200
committerAbigail <abigail@abigail.be>2020-10-03 17:08:44 +0200
commitbcfbb8da7e0070de6b6dc53ec6f040a966a759d5 (patch)
treefc641facf6bbd7b6247044b88781a4b6e245f907 /challenge-080
parent3dde1ee107e04f7009d613b048d4978e743e415a (diff)
downloadperlweeklychallenge-club-bcfbb8da7e0070de6b6dc53ec6f040a966a759d5.tar.gz
perlweeklychallenge-club-bcfbb8da7e0070de6b6dc53ec6f040a966a759d5.tar.bz2
perlweeklychallenge-club-bcfbb8da7e0070de6b6dc53ec6f040a966a759d5.zip
Perl solution for week 80/challenge 2.
Diffstat (limited to 'challenge-080')
-rw-r--r--challenge-080/abigail/perl/ch-2.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-080/abigail/perl/ch-2.pl b/challenge-080/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..616587bcde
--- /dev/null
+++ b/challenge-080/abigail/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+
+#
+#
+# Challenge 2:
+#
+# You are given rankings of @N candidates.
+#
+# Write a script to find out the total candies needed for all candidates.
+# You are asked to follow the rules below:
+# a) You must given at least one candy to each candidate.
+# b) Candidate with higher ranking get more candies than their immediate
+# neighbors on either side.
+#
+
+#
+# Note:
+#
+# - Each candidate gets 1, 2, or 3 candies. If a candidates ranking is
+# higher than each of its neighbours, it gets 3 candies. If its ranking
+# is higher than only on of its neighbours, it gets 2 candies. Else, it
+# gets 1 candy.
+#
+# - It is not given that rankings are positive integers, or even integers.
+# They may be reals.
+#
+
+
+while (<>) {
+ my $candies = 0;
+ #
+ # Read a line of input, split on whitespace, and put the results
+ # in an array @N.
+ #
+ my @N = split ' ';
+
+ #
+ # Iterate over the array, and count the candies.
+ #
+ foreach my $i (keys @N) {
+ $candies ++;
+ $candies ++ if $i > 0 && $N [$i] > $N [$i - 1];
+ $candies ++ if $i < $#N && $N [$i] > $N [$i + 1];
+ }
+
+ say $candies;
+}
+
+__END__