diff options
| author | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-09-05 09:29:08 -0600 |
|---|---|---|
| committer | Mark A <andemark@a-iot1t.uch.ad.pvt> | 2021-09-05 09:29:08 -0600 |
| commit | 9a80c688e960425f95349086dac048adaaa892a6 (patch) | |
| tree | f7c46ec39337a43af62653433af846abbce1bc97 | |
| parent | 9170f8a547d40cf4d470b65aa769027a4ffcaf10 (diff) | |
| download | perlweeklychallenge-club-9a80c688e960425f95349086dac048adaaa892a6.tar.gz perlweeklychallenge-club-9a80c688e960425f95349086dac048adaaa892a6.tar.bz2 perlweeklychallenge-club-9a80c688e960425f95349086dac048adaaa892a6.zip | |
ch-2.raku fixed
| -rw-r--r-- | challenge-128/mark-anderson/raku/ch-1.raku | 6 | ||||
| -rw-r--r-- | challenge-128/mark-anderson/raku/ch-2.raku | 76 |
2 files changed, 69 insertions, 13 deletions
diff --git a/challenge-128/mark-anderson/raku/ch-1.raku b/challenge-128/mark-anderson/raku/ch-1.raku index f33ab749ad..dad21d38c4 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'; diff --git a/challenge-128/mark-anderson/raku/ch-2.raku b/challenge-128/mark-anderson/raku/ch-2.raku index 0b54764864..674478d75e 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)); + } + } +} |
