aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-04 12:03:54 +0000
committerGitHub <noreply@github.com>2023-03-04 12:03:54 +0000
commit2492a97b5e15f22b3ad04a2bf0c29730fc4ab4a6 (patch)
tree01388ab3739555cc2fc915d2d8c3742cf088e730
parent0e5b6eecd54750b06b9ee5196069ecaf5b9690db (diff)
parent08c28c81508d305907fde10a50c9c5c09773b360 (diff)
downloadperlweeklychallenge-club-2492a97b5e15f22b3ad04a2bf0c29730fc4ab4a6.tar.gz
perlweeklychallenge-club-2492a97b5e15f22b3ad04a2bf0c29730fc4ab4a6.tar.bz2
perlweeklychallenge-club-2492a97b5e15f22b3ad04a2bf0c29730fc4ab4a6.zip
Merge pull request #7657 from polettix/polettix/pwc206
Add polettix's solution to challenge-206
-rw-r--r--challenge-206/polettix/blog.txt1
-rw-r--r--challenge-206/polettix/blog1.txt1
-rw-r--r--challenge-206/polettix/perl/ch-1.pl19
-rw-r--r--challenge-206/polettix/perl/ch-2.pl4
-rw-r--r--challenge-206/polettix/raku/ch-1.raku13
-rw-r--r--challenge-206/polettix/raku/ch-2.raku3
6 files changed, 41 insertions, 0 deletions
diff --git a/challenge-206/polettix/blog.txt b/challenge-206/polettix/blog.txt
new file mode 100644
index 0000000000..455a776e8c
--- /dev/null
+++ b/challenge-206/polettix/blog.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2023/03/02/pwc206-shortest-time/
diff --git a/challenge-206/polettix/blog1.txt b/challenge-206/polettix/blog1.txt
new file mode 100644
index 0000000000..5eb267275c
--- /dev/null
+++ b/challenge-206/polettix/blog1.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2023/03/03/pwc206-array-pairings/
diff --git a/challenge-206/polettix/perl/ch-1.pl b/challenge-206/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..4c4c8d23da
--- /dev/null
+++ b/challenge-206/polettix/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+
+say shortest_time(@ARGV);
+
+sub shortest_time {
+ my @times = map { my ($h, $m) = split m{:}mxs; $h * 60 + $m } @_
+ or return;
+ my $period = 24 * 60;
+ my $min = $period;
+ for my $i (0 .. $#times - 1) {
+ for my $j ($i + 1 .. $#times) {
+ my $delta = ($times[$i] - $times[$j]) % $period;
+ $min = ($_ < $min ? $_ : $min) for ($delta, $period - $delta);
+ }
+ }
+ return $min;
+}
diff --git a/challenge-206/polettix/perl/ch-2.pl b/challenge-206/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..4d281f39bd
--- /dev/null
+++ b/challenge-206/polettix/perl/ch-2.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use List::Util qw< pairkeys sum >;
+print array_pairings(@ARGV), "\n";
+sub array_pairings { sum pairkeys sort { $a <=> $b } @_ }
diff --git a/challenge-206/polettix/raku/ch-1.raku b/challenge-206/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..1ce2dfaedc
--- /dev/null
+++ b/challenge-206/polettix/raku/ch-1.raku
@@ -0,0 +1,13 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (*@args) { put shortest-time(@args) }
+
+sub shortest-time (@times) {
+ my \period = 24*60;
+ @times
+ .map({(.comb(/\d+/)».Int «*» (60, 1)).sum}) # turn everything into minutes
+ .combinations(2) # create all possible pairs
+ .map(->($x, $y) { ($x - $y) % period }) # calculate difference, modulo "period"
+ .map({min($^x, period - $^x)}) # consider that and its reciprocal
+ .min # take the minimum, as requested
+}
diff --git a/challenge-206/polettix/raku/ch-2.raku b/challenge-206/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..b8f2c0d483
--- /dev/null
+++ b/challenge-206/polettix/raku/ch-2.raku
@@ -0,0 +1,3 @@
+#!/usr/bin/env raku
+sub MAIN (*@args) { put array-pairings(@args) }
+sub array-pairings (Array[Int]() $array) { $array.sort[0, 2 ... *].sum }