diff options
| author | Mark <53903062+andemark@users.noreply.github.com> | 2023-10-03 01:30:25 +0000 |
|---|---|---|
| committer | Mark <53903062+andemark@users.noreply.github.com> | 2023-10-03 01:30:25 +0000 |
| commit | 3ef6bb41dfa131467507f1de256dfa0ddd839bf1 (patch) | |
| tree | 30c898a0d312145f52836492e70d32c9d1436ba1 /challenge-237 | |
| parent | 9a50910d2d97f98fc942e1a5275016a462be8abb (diff) | |
| download | perlweeklychallenge-club-3ef6bb41dfa131467507f1de256dfa0ddd839bf1.tar.gz perlweeklychallenge-club-3ef6bb41dfa131467507f1de256dfa0ddd839bf1.tar.bz2 perlweeklychallenge-club-3ef6bb41dfa131467507f1de256dfa0ddd839bf1.zip | |
Challenge 237 Solutions (Raku)
Diffstat (limited to 'challenge-237')
| -rw-r--r-- | challenge-237/mark-anderson/raku/WDOM.rakumod | 28 | ||||
| -rw-r--r-- | challenge-237/mark-anderson/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-237/mark-anderson/raku/ch-2.raku | 21 |
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-237/mark-anderson/raku/WDOM.rakumod b/challenge-237/mark-anderson/raku/WDOM.rakumod new file mode 100644 index 0000000000..8e96b4b280 --- /dev/null +++ b/challenge-237/mark-anderson/raku/WDOM.rakumod @@ -0,0 +1,28 @@ +unit class WDOM is Date is export; + +multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month eq 'L'|'Last') +{ + my $dt = Date.new(:$year, :$month).last-date-in-month; + my $d = $dt.day-of-week - $day-of-week; + $dt - ($d >= 0 ?? $d !! 7 + $d) +} + +multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month ~~ 1..4) +{ + my $dt = Date.new(:$year, :$month); + my $wdom = $day-of-week >= $dt.day-of-week ?? $weekday-of-month - 1 + !! $weekday-of-month; + $dt += 7 * $wdom + $day-of-week - $dt.day-of-week +} + +multi method new(:$weekday-of-month!, :$day-of-week!, :$month!, :$year! where $weekday-of-month == 5) +{ + my $dt = Date.new(:$year, :$month).last-date-in-month; + my $d = $dt.day-of-week - $day-of-week; + $dt -= $d >= 0 ?? $d !! 7 + $d; + + return $dt if $dt.day == 29|30|31; + + # Todo: Handle the exception here. Just return the 4th day-of-week? + die "There isn't a 5th $day-of-week in $month $year" +} diff --git a/challenge-237/mark-anderson/raku/ch-1.raku b/challenge-237/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..760b1a5807 --- /dev/null +++ b/challenge-237/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use lib '.'; +use WDOM; +use Test; + +is seize-the-day(2024, 4, 3, 2), 16; +is seize-the-day(2025, 10, 2, 4), 9; +is seize-the-day(2026, 8, 5, 3), 0; + +sub seize-the-day($year, $month, $weekday-of-month, $day-of-week) +{ + quietly + try WDOM.new(:$weekday-of-month, :$day-of-week, :$month, :$year).day +} diff --git a/challenge-237/mark-anderson/raku/ch-2.raku b/challenge-237/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..875abf6529 --- /dev/null +++ b/challenge-237/mark-anderson/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku +use Test; + +is maximise-greatness(1,3,5,2,1,3,1), 4; +is maximise-greatness(1,2,3,4), 3; +is maximise-greatness(1,3,5,2,1,3,1,5), 5; +is maximise-greatness(4,5,5,5,5), 1; +is maximise-greatness(3,3,3,3,3), 0; + +sub maximise-greatness(*@n) +{ + @n .= sort; + my $i = 0; + + sum do while @n[0] < @n[*-1] and $i < @n + { + $i++ while @n[0] == @n[$i]; + @n.shift; + 1 + } +} |
