diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-30 10:22:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-30 10:22:52 +0100 |
| commit | 03da2e8e72831fd2a914b7c514310b1c66d462d4 (patch) | |
| tree | bdfa7a0a5c435254b692c4ecb94c05dd5813306d | |
| parent | 9cb0f827d5de349f7e135514d1e1333af6302680 (diff) | |
| parent | 11309638a2d6276b0316a58627d46fd6fa8b9a2e (diff) | |
| download | perlweeklychallenge-club-03da2e8e72831fd2a914b7c514310b1c66d462d4.tar.gz perlweeklychallenge-club-03da2e8e72831fd2a914b7c514310b1c66d462d4.tar.bz2 perlweeklychallenge-club-03da2e8e72831fd2a914b7c514310b1c66d462d4.zip | |
Merge pull request #8159 from wlmb/master
Solve PWC219
| -rw-r--r-- | challenge-219/wlmb/blog.txt | 2 | ||||
| -rwxr-xr-x | challenge-219/wlmb/perl/ch-1.pl | 13 | ||||
| -rwxr-xr-x | challenge-219/wlmb/perl/ch-2.pl | 25 |
3 files changed, 40 insertions, 0 deletions
diff --git a/challenge-219/wlmb/blog.txt b/challenge-219/wlmb/blog.txt new file mode 100644 index 0000000000..4b59e70921 --- /dev/null +++ b/challenge-219/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/05/29/PWC219/ + diff --git a/challenge-219/wlmb/perl/ch-1.pl b/challenge-219/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..4f4d3e4743 --- /dev/null +++ b/challenge-219/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 219 +# Task 1: Sorted Squares +# +# See https://wlmb.github.io/2023/05/29/PWC219/#task-1-sorted-squares +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 -- N1 [N2...] + to square and sort the numbers N1 N2... + The -- is to allow negative numbers as arguments without + confusing them with options. + FIN +say "@ARGV ->", join " ", sort {$a<=>$b} map {$_**2} @ARGV; diff --git a/challenge-219/wlmb/perl/ch-2.pl b/challenge-219/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..8e8602097a --- /dev/null +++ b/challenge-219/wlmb/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# Perl weekly challenge 219 +# Task 2: Travel Expenditure +# +# See https://wlmb.github.io/2023/05/29/PWC219/#task-2-travel-expenditure +use v5.36; +use List::Util qw(min); +die <<~"FIN" unless @ARGV > 3; + Usage: $0 C1 C7 C30 D1 [D2...] + to calculate the travel expenditure for days D1, D2... + given the costs C1 C7 and C30 for one, seven and thirty day passes + FIN +my @kinds=(1,7,30); # kinds of passes +my @days=@ARGV; +my @costs=splice @days, 0, 3; +my $expenditure=cost(0, @days); +say "Costs: @costs, Days: @days, Expenditure: $expenditure"; + +sub cost($current, @days){ + shift @days while @days and $current >= $days[0]; + return 0 unless @days; + my $now=shift(@days); + my $cost=min map {$costs[$_]+cost($now+$kinds[$_]-1, @days)} 0..2; + return $cost; +} |
