aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-09-05 20:56:06 +0100
committerGitHub <noreply@github.com>2021-09-05 20:56:06 +0100
commit3f16604c743bc8227c65c86ab1c1e7eeede3bd46 (patch)
tree50e38d9b53157a8a5774bddf6fcee959536c90f0
parent2dd78af3a0d6da9552a508f12e312b9d017ef826 (diff)
parent1ec10751b9a7ad529451987c583e3d6d1508a3af (diff)
downloadperlweeklychallenge-club-3f16604c743bc8227c65c86ab1c1e7eeede3bd46.tar.gz
perlweeklychallenge-club-3f16604c743bc8227c65c86ab1c1e7eeede3bd46.tar.bz2
perlweeklychallenge-club-3f16604c743bc8227c65c86ab1c1e7eeede3bd46.zip
Merge pull request #4839 from andemark/branch-for-challenge-128
ch-2.raku fixed
-rw-r--r--challenge-128/mark-anderson/raku/ch-1.raku8
-rw-r--r--challenge-128/mark-anderson/raku/ch-2.raku76
2 files changed, 70 insertions, 14 deletions
diff --git a/challenge-128/mark-anderson/raku/ch-1.raku b/challenge-128/mark-anderson/raku/ch-1.raku
index f33ab749ad..a4562cded4 100644
--- a/challenge-128/mark-anderson/raku/ch-1.raku
+++ b/challenge-128/mark-anderson/raku/ch-1.raku
@@ -8,13 +8,13 @@ is-deeply max-sub-matrix(<1 0 0 0 1 0>,
<1 0 0 0 0 0>),
['2 x 3 at rows 1..2 and cols 2..4',
'3 x 2 at rows 0..2 and cols 2..3'],
- 'Example 1';
+ 'Example 1';
is-deeply max-sub-matrix(<0 0 1 1>,
<0 0 0 1>,
<0 0 1 0>),
['3 x 2 at rows 0..2 and cols 0..1'],
- 'Example 2';
+ 'Example 2';
is-deeply max-sub-matrix(<1 0 1 0 1 0 1 0 1 0 0 1>,
<1 0 1 0 1 0 1 0 1 0 0 1>,
@@ -52,7 +52,7 @@ is-deeply max-sub-matrix(
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]),
+ [1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]),
['2 x 9 at rows 13..14 and cols 0..8',
'2 x 9 at rows 16..17 and cols 4..12'],
'E Choroba\'s Matrix';
@@ -63,7 +63,7 @@ sub max-sub-matrix(+@matrix)
my @zero-indices = @matrix.map({ .grep(0, :k) })>>.Array;
- for (^@matrix).combinations: 2 -> ($first-row, $last-row)
+ for (^@zero-indices).combinations: 2 -> ($first-row, $last-row)
{
my @cols = ([(&)] @zero-indices[$first-row .. $last-row]).keys.sort;
next unless @cols > 1;
diff --git a/challenge-128/mark-anderson/raku/ch-2.raku b/challenge-128/mark-anderson/raku/ch-2.raku
index 0b54764864..0cf05bb73d 100644
--- a/challenge-128/mark-anderson/raku/ch-2.raku
+++ b/challenge-128/mark-anderson/raku/ch-2.raku
@@ -1,23 +1,40 @@
#!/usr/bin/env raku
use Test;
-plan 2;
+plan 7;
-is minimum-platforms(<11:20 14:30>, <11:50 15:00>), 1, 'Example 1';
+is minimum-platforms(<11:20 14:30>,
+ <11:50 15:00>), 1;
is minimum-platforms(<10:20 11:00 11:10 12:20 16:20 19:00>,
- <10:30 13:20 12:40 12:50 20:20 21:20>), 3, 'Example 2';
+ <10:30 13:20 12:40 12:50 20:20 21:20>), 3;
+
+is minimum-platforms(<22:30 23:15 00:30 01:00 01:30>,
+ <23:00 23:55 00:50 01:25 02:00>), 1;
+
+is minimum-platforms(<10:00 11:00 11:00 11:30>,
+ <10:30 11:15 11:20 12:00>), 2;
+
+is minimum-platforms(<10:00 11:00 11:00 11:30>,
+ <11:00 11:15 11:20 12:00>), 3;
+
+is minimum-platforms(<23:00 23:10 00:10 23:00 03:00 15:00 21:00 04:20>,
+ <23:30 01:30 03:00 02:10 04:00 16:00 05:00 05:00>), 2;
+
+is minimum-platforms(<21:30 23:00 00:30 09:00 15:00 21:30 23:00 00:30>,
+ <23:30 23:30 03:00 10:30 23:00 23:00 01:00 03:00>), 3;
sub minimum-platforms($arrivals, $departures)
{
- my $result;
+ my @arrivals = arrival-dts($arrivals);
+ my @departures = departure-dts(@arrivals.List, $departures);
+
my $platforms;
- my @arrivals = $arrivals.sort;
- my @departures = $departures.sort;
+ my $result;
while @arrivals
{
- if @arrivals.head lt @departures.head
+ if @arrivals.head <= @departures.head
{
@arrivals.shift;
$result = max $result, ++$platforms;
@@ -26,9 +43,48 @@ sub minimum-platforms($arrivals, $departures)
else
{
@departures.shift;
- $platforms--;
+ --$platforms;
}
+ }
+
+ return $result;
+
+ class dt
+ {
+ has Int $.day;
+ has Str $.time;
}
- $result;
-}
+ multi infix:<< <= >>(dt $a, dt $b)
+ {
+ my $r = $a.day <=> $b.day;
+
+ return True if $r ~~ Less;
+ return False if $r ~~ More;
+
+ return $a.time le $b.time;
+ }
+
+ sub arrival-dts($arrivals)
+ {
+ my $day = 1;
+ my $time = '00:00';
+
+ gather for $arrivals<> -> $t
+ {
+ $day++ if $t lt $time;
+ $time = $t;
+ take dt.new(:$day, :time($t));
+ }
+ }
+
+ sub departure-dts($arrivals, $departures)
+ {
+ gather for $arrivals<> Z $departures<>
+ {
+ my $day = .head.day;
+ $day++ if .tail lt .head.time;
+ take dt.new(:$day, :time(.tail));
+ }
+ }
+}