aboutsummaryrefslogtreecommitdiff
path: root/challenge-206
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-03-06 02:31:02 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-03-06 02:31:02 +0000
commit1f22fde79160cea6f4b671e738929ded8281c160 (patch)
tree2700a869b0e2a7aaaa1067c607f67c35a794e502 /challenge-206
parent1c3bff4c80d0d6e428005426a2b04ffe049ad13a (diff)
downloadperlweeklychallenge-club-1f22fde79160cea6f4b671e738929ded8281c160.tar.gz
perlweeklychallenge-club-1f22fde79160cea6f4b671e738929ded8281c160.tar.bz2
perlweeklychallenge-club-1f22fde79160cea6f4b671e738929ded8281c160.zip
- Added solutions by Avery Adams.
Diffstat (limited to 'challenge-206')
-rwxr-xr-xchallenge-206/avery-adams/perl/ch-1.pl48
-rwxr-xr-xchallenge-206/avery-adams/perl/ch-2.pl10
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-206/avery-adams/perl/ch-1.pl b/challenge-206/avery-adams/perl/ch-1.pl
new file mode 100755
index 0000000000..16ca6b1993
--- /dev/null
+++ b/challenge-206/avery-adams/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+use strict;
+use v5.10;
+
+use DateTime;
+
+my @times = @ARGV;
+my $closest;
+
+if (scalar @times < 2) {
+ say "I didn't get enough times to process.";
+} else {
+ for (my $index1 = 0; $index1 <= scalar @times - 2; $index1++) {
+ for (my $index2 = $index1 + 1; $index2 <= scalar @times - 1; $index2++) {
+ my ($hours1, $minutes1) = ($times[$index1] =~ /(\d\d):(\d\d)/);
+ my ($hours2, $minutes2) = ($times[$index2] =~ /(\d\d):(\d\d)/);
+ my $datetime1 = DateTime->new(
+ year => 2023,
+ day => 1,
+ hour => $hours1,
+ minute => $minutes1
+ );
+ my $datetime2 = DateTime->new(
+ year => 2023,
+ day => 2,
+ hour => $hours1,
+ minute => $minutes1
+ );
+ my $datetime3 = DateTime->new(
+ year => 2023,
+ day => 1,
+ hour => $hours2,
+ minute => $minutes2
+ );
+ my $delta1 = $datetime3->delta_ms($datetime1);
+ my $delta2 = $datetime3->delta_ms($datetime2);
+ if ($delta1->in_units('minutes') <= $delta2->in_units('minutes')) {
+ if ($delta1->in_units('minutes') < $closest) {$closest = $delta1->in_units('minutes')};
+ if (!defined($closest)) {$closest = $delta1->in_units('minutes')};
+ } else {
+ if ($delta2->in_units('minutes') < $closest) {$closest = $delta2->in_units('minutes')};
+ if (!defined($closest)) {$closest = $delta2->in_units('minutes')};
+ }
+ }
+ }
+ say "The closest numbers are $closest minutes apart.";
+}
diff --git a/challenge-206/avery-adams/perl/ch-2.pl b/challenge-206/avery-adams/perl/ch-2.pl
new file mode 100755
index 0000000000..f822d27541
--- /dev/null
+++ b/challenge-206/avery-adams/perl/ch-2.pl
@@ -0,0 +1,10 @@
+#!/usr/bin/perl
+
+use strict;
+use v5.10;
+
+my @list = @ARGV;
+if (@list % 2) {say "Not an even number of items." && exit};
+my $split = @list / 2;
+my @sorted = sort { $a <=> $b } @list;
+say $sorted[0] + $sorted[$split]; \ No newline at end of file