From 889d54e9da3132ac4eb3eea9bdbb36b5fe3e423b Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 2 Sep 2024 12:51:14 -0600 Subject: Solve PWC 285 --- challenge-285/wlmb/blog.txt | 1 + challenge-285/wlmb/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-285/wlmb/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 challenge-285/wlmb/blog.txt create mode 100755 challenge-285/wlmb/perl/ch-1.pl create mode 100755 challenge-285/wlmb/perl/ch-2.pl diff --git a/challenge-285/wlmb/blog.txt b/challenge-285/wlmb/blog.txt new file mode 100644 index 0000000000..951aa5de7d --- /dev/null +++ b/challenge-285/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2024/09/02/PWC285/ diff --git a/challenge-285/wlmb/perl/ch-1.pl b/challenge-285/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..f1421e4b37 --- /dev/null +++ b/challenge-285/wlmb/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 285 +# Task 1: No Connection +# +# See https://wlmb.github.io/2024/09/02/PWC285/#task-1-no-connection +use v5.36; +use experimental qw(for_list); +die <<~"FIN" unless @ARGV and @ARGV%2==0; + Usage: $0 I1 O1 I2 O2... + where Ii is the incoming and Oi the outgoing node + of the i-th edge. + FIN +my (%in, %out); +for my($in,$out)(@ARGV){ + ++$in{$in} if $in ne $out; + ++$out{$out}; +} +say "@ARGV -> ", join " ", grep{!$in{$_}}keys %out; diff --git a/challenge-285/wlmb/perl/ch-2.pl b/challenge-285/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..9e62d1d808 --- /dev/null +++ b/challenge-285/wlmb/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# Perl weekly challenge 285 +# Task 2: Making Change +# +# See https://wlmb.github.io/2024/09/02/PWC285/#task-2-making-change +use v5.36; +use Memoize; +die <<~"FIN" unless @ARGV; + Usage: $0 A1 A2... + to find in how many ways can the amounts Ai can be + formed with coins (pennies, nickels, dimes, quartes + and half-dollars) + FIN +memoize qw(number_of_ways); +my @values=(1,5,10,25,50); # sorted +my $large=1+$values[-1]; # larger than largest coin +for(@ARGV){ + say "$_ -> ",number_of_ways($_+$large, $large) +} +sub number_of_ways($amount,$first_coin){ + return 0 if $first_coin>$amount; + return 1 if $first_coin==$amount; + my $total; + $total+=number_of_ways($amount-$first_coin, $_)for grep{$_<=$first_coin}@values; + return $total; +} -- cgit