diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2023-05-29 11:10:29 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2023-05-29 11:10:29 -0600 |
| commit | 11309638a2d6276b0316a58627d46fd6fa8b9a2e (patch) | |
| tree | b3c74beaaeb930b0cf99aa4f24bf88ec7e42e54d | |
| parent | 979144e452a65703e7845a166d7c94ba6f89e37f (diff) | |
| download | perlweeklychallenge-club-11309638a2d6276b0316a58627d46fd6fa8b9a2e.tar.gz perlweeklychallenge-club-11309638a2d6276b0316a58627d46fd6fa8b9a2e.tar.bz2 perlweeklychallenge-club-11309638a2d6276b0316a58627d46fd6fa8b9a2e.zip | |
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; +} |
