aboutsummaryrefslogtreecommitdiff
path: root/challenge-210
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-28 09:43:56 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-28 09:48:55 +0100
commitc62ba86c52ad00d866516834828dd7731c63d551 (patch)
tree83ee0192897d05edf57acacb803dce4c87bfef0e /challenge-210
parent1f0e0a061b0e758d9a10010c573443f1dcf9ae20 (diff)
downloadperlweeklychallenge-club-c62ba86c52ad00d866516834828dd7731c63d551.tar.gz
perlweeklychallenge-club-c62ba86c52ad00d866516834828dd7731c63d551.tar.bz2
perlweeklychallenge-club-c62ba86c52ad00d866516834828dd7731c63d551.zip
Add Perl solution
Diffstat (limited to 'challenge-210')
-rw-r--r--challenge-210/paulo-custodio/Makefile2
-rw-r--r--challenge-210/paulo-custodio/perl/ch-1.pl52
-rw-r--r--challenge-210/paulo-custodio/perl/ch-2.pl74
-rw-r--r--challenge-210/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-210/paulo-custodio/t/test-2.yaml15
5 files changed, 153 insertions, 0 deletions
diff --git a/challenge-210/paulo-custodio/Makefile b/challenge-210/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-210/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-210/paulo-custodio/perl/ch-1.pl b/challenge-210/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..d7ff8dc143
--- /dev/null
+++ b/challenge-210/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+
+# Challenge 210
+#
+# Task 1: Kill and Win
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of integers.
+#
+# Write a script to get the maximum points. You are allowed to take out (kill)
+# any integer and remove from the list. However if you do that then all
+# integers exactly one-less or one-more would also be removed. Find out the
+# total of integers removed.
+# Example 1
+#
+# Input: @int = (2, 3, 1)
+# Output: 6
+#
+# First we delete 2 and that would also delete 1 and 3. So the maximum points
+# we get is 6.
+#
+# Example 2
+#
+# Input: @int = (1, 1, 2, 2, 2, 3)
+# Output: 11
+#
+# First we delete 2 and that would also delete both the 1's and the 3. Now we
+# have (2, 2).
+# Then we delete another 2 and followed by the third deletion of 2. So the
+# maximum points we get is 11.
+
+use Modern::Perl;
+use List::Util 'sum';
+
+sub kill_and_win {
+ my(@n)=@_;
+ return 0 if @n==0;
+ # kill one and recurse
+ my $max=0;
+ for my $i (0..$#n) {
+ my $kill=$n[$i];
+ my @copy=@n;
+ splice(@copy,$i,1); # remove kill value
+ my $value=$kill+sum(0,grep {$_==$kill+1 || $_==$kill-1} @copy);
+ @copy=grep {$_!=$kill+1 && $_!=$kill-1} @copy; # remove above and below
+ $value+=kill_and_win(@copy);
+ $max=$value if $max<$value;
+ }
+ return $max;
+}
+
+say kill_and_win(@ARGV);
diff --git a/challenge-210/paulo-custodio/perl/ch-2.pl b/challenge-210/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..b9b98eeb6f
--- /dev/null
+++ b/challenge-210/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+
+# Challenge 210
+#
+# Task 2: Number Collision
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers which can move in right direction if it is
+# positive and left direction when negative. If two numbers collide then the
+# smaller one will explode. And if both are same then they both explode. We
+# take the absolute value in consideration when comparing.
+#
+# All numbers move at the same speed, therefore any 2 numbers moving in the same
+# direction will never collide.
+#
+# Write a script to find out who survives the collision.
+# Example 1:
+#
+# Input: @list = (2, 3, -1)
+# Output: (2, 3)
+#
+# The numbers 3 and -1 collide and -1 explodes in the end. So we are left
+# with (2, 3).
+#
+# Example 2:
+#
+# Input: @list = (3, 2, -4)
+# Output: (-4)
+#
+# The numbers 2 and -4 collide and 2 explodes in the end. That gives us (3, -4).
+# Now the numbers 3 and -4 collide and 3 explodes. Finally we are left with -4.
+#
+# Example 3:
+#
+# Input: @list = (1, -1)
+# Output: ()
+#
+# The numbers 1 and -1 both collide and explode. Nothing left in the end.
+
+use Modern::Perl;
+
+sub find_collision {
+ my(@n)=@_;
+ for my $i (0..$#n-1) {
+ if ($n[$i]>0 && $n[$i+1]<0 ||
+ $n[$i]<0 && $n[$i+1]>0) {
+ return $i;
+ }
+ }
+ return -1;
+}
+
+sub number_collision {
+ my(@n)=@_;
+ my $i;
+ while (($i=find_collision(@n))>=0) {
+ if (abs($n[$i])==abs($n[$i+1])) {
+ splice(@n,$i,2); # both explode
+ }
+ elsif (abs($n[$i])>abs($n[$i+1])) {
+ splice(@n,$i+1,1); # right explodes
+ }
+ elsif (abs($n[$i])<abs($n[$i+1])) {
+ splice(@n,$i,1); # left explodes
+ }
+ else { die; }
+ }
+ return @n;
+}
+
+@ARGV or die "usage: ch-2.pl nums...\n";
+my @n=@ARGV;
+@n=number_collision(@n);
+say "(",join(", ", @n),")";
diff --git a/challenge-210/paulo-custodio/t/test-1.yaml b/challenge-210/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..9c6708f823
--- /dev/null
+++ b/challenge-210/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 2 3 1
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: 1 1 2 2 2 3
+ input:
+ output: 11
diff --git a/challenge-210/paulo-custodio/t/test-2.yaml b/challenge-210/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..9a90257033
--- /dev/null
+++ b/challenge-210/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2 3 -1
+ input:
+ output: (2, 3)
+- setup:
+ cleanup:
+ args: 3 2 -4
+ input:
+ output: (-4)
+- setup:
+ cleanup:
+ args: 1 -1
+ input:
+ output: ()