diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-06-04 22:33:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-04 22:33:42 +0100 |
| commit | d493b4ed5dd10a018b8473210efdeed9a4fea224 (patch) | |
| tree | ef6ecd68f9d830304e5f97fd54fa09ba835174dc | |
| parent | 76db955d662fba216b231f0522edf95d2a38efb9 (diff) | |
| parent | 8ede0cb9a43b9a1e61a1d81ca3c2a551db3aadee (diff) | |
| download | perlweeklychallenge-club-d493b4ed5dd10a018b8473210efdeed9a4fea224.tar.gz perlweeklychallenge-club-d493b4ed5dd10a018b8473210efdeed9a4fea224.tar.bz2 perlweeklychallenge-club-d493b4ed5dd10a018b8473210efdeed9a4fea224.zip | |
Merge pull request #8182 from Util/c219
Add TWC 219 solutions by Bruce Gray (Raku only).
| -rw-r--r-- | challenge-219/bruce-gray/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-219/bruce-gray/raku/ch-2.raku | 32 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-219/bruce-gray/raku/ch-1.raku b/challenge-219/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..f2f6da515c --- /dev/null +++ b/challenge-219/bruce-gray/raku/ch-1.raku @@ -0,0 +1,12 @@ +sub task1 { @^in»².sort } + + +my @tests = + ( ( -2, -1, 0, 3, 4 ) , ( 0, 1, 4, 9, 16 ) ), + ( ( 5, -4, -1, 3, 6 ) , ( 1, 9, 16, 25, 36 ) ), +; +use Test; +plan +@tests; +for @tests -> ( $in, $expected ) { + is-deeply task1($in), $expected; +} diff --git a/challenge-219/bruce-gray/raku/ch-2.raku b/challenge-219/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..85b73d85ab --- /dev/null +++ b/challenge-219/bruce-gray/raku/ch-2.raku @@ -0,0 +1,32 @@ +# This is just my translation of https://leetcode.com/problems/minimum-cost-for-tickets/editorial/ +# into Raku. I should receive reduced street-cred for it. :^) +sub task2 ( @costs, @days ) { + constant @duration = 1, 7, 30; + die if @costs != @duration; + my @cd = @costs Z @duration; + + warn "No advantage to 7-day ticket over 1-day" if @costs[0] * 7 <= @costs[1]; + warn "No advantage to 30-day ticket over 1-day" if @costs[0] * 30 <= @costs[2]; + warn "No advantage to 30-day ticket over combo" if @costs[1] * 4 + + @costs[0] * 2 <= @costs[2]; + + my Set $dayset = @days.Set; + my UInt $max = @days.max; + + my sub dp ( $i ) { + return $i > $max ?? 0 + !! $i ∉ $dayset ?? dp($i + 1) + !! @cd.map({ dp($i + .[1]) + .[0] }).min; + } + + return dp(1); +} + + +my @tests = map { %( <expected costs days> Z=> .list ) }, + ( 11, (2, 7, 25), (1, 5, 6, 7, 9, 15) ), + ( 20, (2, 7, 25), (1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31) ), +; +use Test; +plan +@tests; +is task2(.<costs>, .<days>), .<expected> for @tests; |
