diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-31 22:07:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-31 22:07:27 +0100 |
| commit | 3ce5f132262d3ddc09e66beab26cfcada9f9bde6 (patch) | |
| tree | 707e10cf795380299790b84420e780498efe1424 | |
| parent | f001af2be3d5609a53891307a0075d1b223077ba (diff) | |
| parent | 8b41075a38dd7c8b67f86ed7c389eb442990dcc0 (diff) | |
| download | perlweeklychallenge-club-3ce5f132262d3ddc09e66beab26cfcada9f9bde6.tar.gz perlweeklychallenge-club-3ce5f132262d3ddc09e66beab26cfcada9f9bde6.tar.bz2 perlweeklychallenge-club-3ce5f132262d3ddc09e66beab26cfcada9f9bde6.zip | |
Merge pull request #8166 from andemark/challenge-219-Raku
Initial 219 (Raku)
| -rw-r--r-- | challenge-219/mark-anderson/raku/ch-1.raku | 10 | ||||
| -rw-r--r-- | challenge-219/mark-anderson/raku/ch-2.raku | 51 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-219/mark-anderson/raku/ch-1.raku b/challenge-219/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..557f716d45 --- /dev/null +++ b/challenge-219/mark-anderson/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Test; + +is-deeply sorted-squares(-2,-1,0,3,4), (0,1,4,9,16); +is-deeply sorted-squares(5,-4,-1,3,6), (1,9,16,25,36); + +sub sorted-squares(+$a) +{ + sort $a >>**>> 2 +} diff --git a/challenge-219/mark-anderson/raku/ch-2.raku b/challenge-219/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..c59f4dcbce --- /dev/null +++ b/challenge-219/mark-anderson/raku/ch-2.raku @@ -0,0 +1,51 @@ +#!/usr/bin/env raku +use Test; + +is travel-expenditure(1,5,6,7,9,15), 11; +is travel-expenditure(1,2,3,5,7,10,11,12,14,20,30,31), 20; +is travel-expenditure(1..2500), 2088; +is travel-expenditure(1..2520), 2100; +is travel-expenditure(1..2525), 2107; +is travel-expenditure(1..2528), 2109; + +# Not the most elegant solution but it's pretty fast. + +sub travel-expenditure(+@days) +{ + my $cost; + my %cost := { 1 => 2, 7 => 7, 30 => 25 } + my %min-span := { 1 => 1, 7 => 4, 30 => 23 } + + for (30,7,1) -> $n + { + loop + { + last unless @days; + my %days = @days.antipairs; + my @mm = @days.minmax.rotor($n => $n-1); + + unless @mm + { + delete-days(@days) if @days.minmax >= %min-span{$n}; + last + } + + my @ints = do ($_ (&) @days).keys for @mm; + + my %c = @ints.classify({ .elems }); + + last unless %c.max.key >= %min-span{$n}; + + delete-days(%c.max.value.head); + + sub delete-days(@a) + { + %days{@a}:delete; + @days = %days.keys>>.Int; + $cost += %cost{$n}; + } + } + } + + $cost +} |
