aboutsummaryrefslogtreecommitdiff
path: root/challenge-285
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2024-09-06 21:39:57 -0700
committerrobbie-hatley <Robbie.Hatley@gmail.com>2024-09-06 21:39:57 -0700
commiteff17a6785c5096649d4af81af351a88d71b61e4 (patch)
tree2bff08bff21f05dadeaa2960c29c4e1e7eb90b42 /challenge-285
parent0c9d8d680098f5515616488eceedbcfae8c5fea7 (diff)
downloadperlweeklychallenge-club-eff17a6785c5096649d4af81af351a88d71b61e4.tar.gz
perlweeklychallenge-club-eff17a6785c5096649d4af81af351a88d71b61e4.tar.bz2
perlweeklychallenge-club-eff17a6785c5096649d4af81af351a88d71b61e4.zip
Robbie Hatley's Perl solutions for The Weekly Challenge #285.
Diffstat (limited to 'challenge-285')
-rw-r--r--challenge-285/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-285/robbie-hatley/perl/ch-1.pl75
-rwxr-xr-xchallenge-285/robbie-hatley/perl/ch-2.pl75
3 files changed, 151 insertions, 0 deletions
diff --git a/challenge-285/robbie-hatley/blog.txt b/challenge-285/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..07224b2063
--- /dev/null
+++ b/challenge-285/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/09/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file
diff --git a/challenge-285/robbie-hatley/perl/ch-1.pl b/challenge-285/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..e4f1df2c7b
--- /dev/null
+++ b/challenge-285/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env -S perl -C63
+
+=pod
+
+The Weekly Challenge #285-1: "No Connection"
+Submitted by: Mohammad Sajid Anwar
+You are given a list of routes, @routes. Write a script to find
+the destination with no further outgoing connection.
+
+Example 1:
+Input: @routes = (["B","C"], ["D","B"], ["C","A"])
+Output: "A"
+"D" -> "B" -> "C" -> "A".
+"B" -> "C" -> "A".
+"C" -> "A".
+"A".
+
+Example 2:
+Input: @routes = (["A","Z"])
+Output: "Z"
+
+Solution in Perl written by Robbie Hatley on Fri Sep 06, 2024.
+
+Input is by @ARGV or internal array. If using @ARGV, provide one input which must be a single-quoted array of
+arrays of arrays of route nodes, in proper Perl syntax, like so:
+./ch-1.pl '([["A","B","C"],["B","A","C"],["C","B"]],[["A","E","X"],["X","E","F"],["A","B","C","A"]])'
+
+Output is to STDOUT and will be each input followed by its corresponding output.
+
+=cut
+
+use v5.36;
+use utf8;
+$"=', ';
+
+sub deadends ($route_set_ref) {
+ my %live;
+ my %dead;
+ my @routes = @$route_set_ref;
+ for my $route (@routes) {
+ my @nodes = @$route;
+ for my $i (0..$#nodes) {
+ if ($i < $#nodes) {
+ if (not defined $live{$nodes[$i]}) {
+ $live{$nodes[$i]} = 1;
+ }
+ }
+ }
+ }
+ for my $route (@routes) {
+ if (not defined $live{$route->[-1]}) {
+ if (not defined $dead{$route->[-1]}) {
+ $dead{$route->[-1]} = 1;
+ }
+ }
+ }
+ return sort keys %dead;
+}
+
+my @route_sets = @ARGV ? eval($ARGV[0]) :
+(
+ [["B","C"], ["D","B"], ["C","A"]],
+ [["A","Z"]],
+);
+
+for my $route_set_ref (@route_sets) {
+ my @deadends = deadends($route_set_ref);
+ say '';
+ say 'Routes:';
+ say "@$_" for @$route_set_ref;
+ say "Deadends = @deadends";
+}
+
+
+
diff --git a/challenge-285/robbie-hatley/perl/ch-2.pl b/challenge-285/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..8ecabd7721
--- /dev/null
+++ b/challenge-285/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env -S perl -C63
+
+=pod
+
+The Weekly Challenge #285-2: "Making Change"
+Submitted by: David Ferrone
+Compute the number of ways to make change for a given value in
+cents by using the coins Penny, Nickel, Dime, Quarter and
+Half-dollar. Order of coin selection does not matter.
+A penny (P) is equal to 1 cent.
+A nickel (N) is equal to 5 cents.
+A dime (D) is equal to 10 cents.
+A quarter (Q) is equal to 25 cents.
+A half-dollar (H) is equal to 50 cents.
+
+Example 1:
+Input: $amount = 9
+Ouput: 2
+1: 9P
+2: N + 4P
+
+Example 2:
+Input: $amount = 15
+Ouput: 6
+1: D + 5P
+2: D + N
+3: 3N
+4: 2N + 5P
+5: N + 10P
+6: 15P
+
+Example 3:
+Input: $amount = 100
+Ouput: 292
+
+Solution in Perl written by Robbie Hatley on Fri Sep 06, 2024.
+
+Input is by @ARGV or internal array. If using @ARGV, provide one input which must be a single-quoted array of
+non-negative integers in proper Perl syntax, like so:
+./ch-2.pl '(0, 13, 74, 203)'
+
+Output is to STDOUT and will be each input followed by its corresponding output.
+
+=cut
+
+use v5.36;
+use utf8;
+$"=', ';
+
+sub make_change :prototype($) ($x) {
+ return 'Error!' if $x !~ m/^0$|^[1-9][0-9]*$/;
+ my @ways;
+ my $count = 0;
+ for my $H ( 0 .. int($x/50) ) {
+ for my $Q ( 0 .. int(($x-50*$H)/25) ) {
+ for my $D ( 0 .. int(($x-50*$H-25*$Q)/10) ) {
+ for my $N ( 0 .. int(($x-50*$H-25*$Q-10*$D)/5) ) {
+ my $P = $x-50*$H-25*$Q-10*$D-5*$N;
+ ++$count;
+ push @ways, "${count}: ${H}H ${Q}Q ${D}D ${N}N ${P}P";
+ }
+ }
+ }
+ }
+ return @ways;
+}
+
+my @values = @ARGV ? eval($ARGV[0]) : (9, 15, 100);
+
+for my $value (@values) {
+ my @ways = make_change($value);
+ say '';
+ say "Ways of making change for $value¢:";
+ say for @ways;
+}