From c62ba86c52ad00d866516834828dd7731c63d551 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 28 Mar 2023 09:43:56 +0100 Subject: Add Perl solution --- challenge-210/paulo-custodio/Makefile | 2 + challenge-210/paulo-custodio/perl/ch-1.pl | 52 +++++++++++++++++++++ challenge-210/paulo-custodio/perl/ch-2.pl | 74 ++++++++++++++++++++++++++++++ challenge-210/paulo-custodio/t/test-1.yaml | 10 ++++ challenge-210/paulo-custodio/t/test-2.yaml | 15 ++++++ 5 files changed, 153 insertions(+) create mode 100644 challenge-210/paulo-custodio/Makefile create mode 100644 challenge-210/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-210/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-210/paulo-custodio/t/test-1.yaml create mode 100644 challenge-210/paulo-custodio/t/test-2.yaml 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])