diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-31 21:48:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-31 21:48:23 +0100 |
| commit | 661aa43bd4a3fbe607f841cd9f03f3995180ce23 (patch) | |
| tree | 003560a0a07d25987a35e952cc87ceb0b373525f | |
| parent | 886ac3cb8c494ec37317d36a32f209424187f34a (diff) | |
| parent | 69585a684d66ca928fd0b675fcf750d5e762013c (diff) | |
| download | perlweeklychallenge-club-661aa43bd4a3fbe607f841cd9f03f3995180ce23.tar.gz perlweeklychallenge-club-661aa43bd4a3fbe607f841cd9f03f3995180ce23.tar.bz2 perlweeklychallenge-club-661aa43bd4a3fbe607f841cd9f03f3995180ce23.zip | |
Merge pull request #8164 from polettix/polettix/pwc219
Add polettix's solution to challenge-219
| -rw-r--r-- | challenge-219/polettix/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-219/polettix/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-219/polettix/perl/ch-1.pl | 3 | ||||
| -rw-r--r-- | challenge-219/polettix/perl/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-219/polettix/raku/ch-1.raku | 3 | ||||
| -rw-r--r-- | challenge-219/polettix/raku/ch-2.raku | 17 |
6 files changed, 49 insertions, 0 deletions
diff --git a/challenge-219/polettix/blog.txt b/challenge-219/polettix/blog.txt new file mode 100644 index 0000000000..d77c72a90d --- /dev/null +++ b/challenge-219/polettix/blog.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/06/01/pwc219-sorted-squares/ diff --git a/challenge-219/polettix/blog1.txt b/challenge-219/polettix/blog1.txt new file mode 100644 index 0000000000..7d3ce05c35 --- /dev/null +++ b/challenge-219/polettix/blog1.txt @@ -0,0 +1 @@ +https://etoobusy.polettix.it/2023/06/02/pwc219-travel-expenditure/ diff --git a/challenge-219/polettix/perl/ch-1.pl b/challenge-219/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..af5cf2e86a --- /dev/null +++ b/challenge-219/polettix/perl/ch-1.pl @@ -0,0 +1,3 @@ +#!/usr/bin/env perl +use v5.24; +say join ', ', sort { $a <=> $b } map { $_ * $_ } @ARGV; diff --git a/challenge-219/polettix/perl/ch-2.pl b/challenge-219/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..07dbe64c94 --- /dev/null +++ b/challenge-219/polettix/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +use Memoize; +no warnings 'recursion'; + +my @days = @ARGV; +my @costs = splice @days, 0, 3; +memoize('travel_expenditure'); +say travel_expenditure(\@costs, @days); + +sub travel_expenditure ($costs, @days) { + state $spans = [1, 7, 30]; + return 0 unless @days; + my $min; + for my $i (0 .. 2) { + my ($first, @pool) = @days; + shift @pool while @pool && $pool[0] < $first + $spans->[$i]; + my $cost = $costs->[$i] + travel_expenditure($costs, @pool); + $min = $cost if (! defined($min)) || ($cost < $min); + } + return $min; +} diff --git a/challenge-219/polettix/raku/ch-1.raku b/challenge-219/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..f65ae75996 --- /dev/null +++ b/challenge-219/polettix/raku/ch-1.raku @@ -0,0 +1,3 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@args) { @args».².sort.join(', ').put } diff --git a/challenge-219/polettix/raku/ch-2.raku b/challenge-219/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..20df5b243e --- /dev/null +++ b/challenge-219/polettix/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use v6; +sub MAIN (*@days is copy) { + put travel-expenditure(@days.splice(0, 3), @days); +} + +sub travel-expenditure (@costs, @days) { + state @spans = 1, 7, 30; + state %cache; + return 0 unless @days; + my $key = @days.join(','); + %cache{$key} //= (@costs Z @spans).map(-> ($cost, $span) { + my ($first, @pool) = @days; + @pool.shift while @pool && @pool[0] < $first + $span; + $cost + samewith(@costs, @pool); + }).min; +} |
