diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-03 01:25:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-03 01:25:42 +0100 |
| commit | 2ee6aa405be90f379f95597ad94e97d1a7985f1d (patch) | |
| tree | f0daeaa9ab24a425706a598e14ba264806ebcb82 /challenge-210 | |
| parent | 77e18efd10255524b9e9daa8c61e40ee0468a54f (diff) | |
| parent | 21fc051888055e71b918de938e97fb38e9209588 (diff) | |
| download | perlweeklychallenge-club-2ee6aa405be90f379f95597ad94e97d1a7985f1d.tar.gz perlweeklychallenge-club-2ee6aa405be90f379f95597ad94e97d1a7985f1d.tar.bz2 perlweeklychallenge-club-2ee6aa405be90f379f95597ad94e97d1a7985f1d.zip | |
Merge pull request #7832 from pme/challenge-210
challenge-210/peter-meszaros
Diffstat (limited to 'challenge-210')
| -rwxr-xr-x | challenge-210/peter-meszaros/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-210/peter-meszaros/perl/ch-2.pl | 65 |
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-210/peter-meszaros/perl/ch-1.pl b/challenge-210/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..4bffae3e76 --- /dev/null +++ b/challenge-210/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr//bin/perl +# 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 strict; +use warnings; +use List::MoreUtils qw(uniq); +use List::Util qw(sum0); +use Test::More; + +my $cases = [ + [2, 3, 1], + [1, 1, 2, 2, 2, 3], +]; + +sub killnum +{ + my $aref = shift; + my @uniq = uniq @$aref; + + my $sum = 0; + for my $n (@uniq) { + my $r = sum0(grep { $_ >= $n-1 or $_ <= $n+1} @$aref); + $sum = $r if $r > $sum; + } + return $sum; +} + +is(killnum($cases->[0]), 6, '[2, 3, 1]'); +is(killnum($cases->[1]), 11, '[1, 1, 2, 2, 2, 3]'); + +done_testing(); + +exit 0; + diff --git a/challenge-210/peter-meszaros/perl/ch-2.pl b/challenge-210/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..abd417757c --- /dev/null +++ b/challenge-210/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr//bin/perl +# 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 strict; +use warnings; +use Test::More; + +my $cases = [ + [2, 3, -1], + [3, 2, -4], + [1, -1], +]; + +sub collision +{ + my @a = (shift)->@*; + my $coll; + do { + $coll = 0; + for my $i (0 .. ($#a-1)) { + my ($x, $y) = ($a[$i], $a[$i+1]); + my $xa = abs($x); + my $ya = abs($y); + if (($x < 0 and $y > 0) or ($x > 0 and $y < 0)) { # collision + ++$coll; + if ($xa == $ya) { + splice(@a, $i, 2); + } elsif ($xa < $ya) { + splice(@a, $i, 1); + } elsif ($xa > $ya) { + splice(@a, $i+1, 1); + } + } + } + } while $coll; + return '(' . join(', ', @a) . ')'; +} + +is(collision($cases->[0]), '(2, 3)', '[2, 3, -1]'); +is(collision($cases->[1]), '(-4)', '[3, 2, -4]'); +is(collision($cases->[2]), '()', '[1, -1]'); +done_testing(); + +exit 0; + |
