From 077442285848686130c6a97b9bb61b4ac2a061de Mon Sep 17 00:00:00 2001 From: Avery Adams Date: Thu, 30 Mar 2023 22:49:22 +1300 Subject: Add solutions by Avery Adams --- challenge-210/avery-adams/perl/ch-1.pl | 15 +++++++++++++++ challenge-210/avery-adams/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-210/avery-adams/perl/ch-1.pl create mode 100644 challenge-210/avery-adams/perl/ch-2.pl diff --git a/challenge-210/avery-adams/perl/ch-1.pl b/challenge-210/avery-adams/perl/ch-1.pl new file mode 100644 index 0000000000..6a63b82bac --- /dev/null +++ b/challenge-210/avery-adams/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use strict; +use v5.10; + +my %hash; +$hash{$_}++ for @ARGV; + +my $max; +for my $int (keys %hash) { + my $total = (($int - 1) * $hash{$int - 1}) + ($int * $hash{$int}) + (($int + 1) * $hash{$int + 1}); + $max = $total if $total > $max or !defined($max); +} + +say $max if defined $max; diff --git a/challenge-210/avery-adams/perl/ch-2.pl b/challenge-210/avery-adams/perl/ch-2.pl new file mode 100644 index 0000000000..41803073d4 --- /dev/null +++ b/challenge-210/avery-adams/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use v5.10; + +my @list = @ARGV; +for (my $index = 0; $index < $#list; $index++) { + my $collision = $list[$index] + $list[$index + 1]; + if ($collision > $list[$index]) {next} + elsif ($collision > 0) { + splice @list, $index + 1, 1; + $index--; + } elsif ($collision == 0) { + splice @list, $index, 2; + $index--; + } elsif (0 > $collision > $list[$index + 1]) { + splice @list, $index, 1; + for (my $index2 = $index - 1; $index2 >= 0 and $list[$index2] > 0; $index2--) { + if (-$list[$index] > $list[$index2]) { + splice @list, $index2, 1; + $index--; + $index2--; + } elsif (-$list[$index] == $list[$index2]) { + splice @list, $index2, 2; + $index -= 2; + } else { + splice @list, $index, 1; + $index--; + } + } + } +} +say $_ for @list; -- cgit From faf0518fdd1805ab468bf26d5017975202110e8c Mon Sep 17 00:00:00 2001 From: Avery Adams Date: Fri, 31 Mar 2023 08:20:29 +1300 Subject: Fix bug in opposite set. --- challenge-210/avery-adams/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-210/avery-adams/perl/ch-2.pl b/challenge-210/avery-adams/perl/ch-2.pl index 41803073d4..2610085fb3 100644 --- a/challenge-210/avery-adams/perl/ch-2.pl +++ b/challenge-210/avery-adams/perl/ch-2.pl @@ -12,7 +12,7 @@ for (my $index = 0; $index < $#list; $index++) { $index--; } elsif ($collision == 0) { splice @list, $index, 2; - $index--; + $index -= 2; } elsif (0 > $collision > $list[$index + 1]) { splice @list, $index, 1; for (my $index2 = $index - 1; $index2 >= 0 and $list[$index2] > 0; $index2--) { -- cgit