aboutsummaryrefslogtreecommitdiff
path: root/challenge-285
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-03 21:46:16 +0100
committerGitHub <noreply@github.com>2024-09-03 21:46:16 +0100
commit522f3e6d27e6d8f1bb2906b169107a9ce27ce60c (patch)
treeb48317456aedf7bc1b18f309cec3ac8fca338e9d /challenge-285
parentc1c8c6dd9a411d5e075f36337856f55a17673912 (diff)
parent908a47e82d354244023c86b2f0b0acd629c293f2 (diff)
downloadperlweeklychallenge-club-522f3e6d27e6d8f1bb2906b169107a9ce27ce60c.tar.gz
perlweeklychallenge-club-522f3e6d27e6d8f1bb2906b169107a9ce27ce60c.tar.bz2
perlweeklychallenge-club-522f3e6d27e6d8f1bb2906b169107a9ce27ce60c.zip
Merge pull request #10768 from pjcs00/wk285
Week 285 - Dead end coins
Diffstat (limited to 'challenge-285')
-rw-r--r--challenge-285/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-285/peter-campbell-smith/perl/ch-1.pl51
-rwxr-xr-xchallenge-285/peter-campbell-smith/perl/ch-2.pl63
3 files changed, 115 insertions, 0 deletions
diff --git a/challenge-285/peter-campbell-smith/blog.txt b/challenge-285/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..5e8e341c9e
--- /dev/null
+++ b/challenge-285/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/285
diff --git a/challenge-285/peter-campbell-smith/perl/ch-1.pl b/challenge-285/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..b1dd302958
--- /dev/null
+++ b/challenge-285/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-09-02
+use utf8; # Week 285 - task 1 - No connection
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+no_connection(['B', 'C'], ['D', 'B'], ['C', 'A']);
+no_connection(['F', 'G'], ['G', 'H'], ['H', 'F']);
+no_connection(['B', 'C'], ['A', 'A'], ['B', 'B']);
+
+# longer example - random routes
+my ($x, $y);
+for (0 .. 24) {
+ $x .= qq{['} . chr(int(rand(15)) + ord('A')) . qq{','} .
+ chr(int(rand(15)) + ord('A')) . qq{'],}
+}
+$x =~ s|.$||;
+no_connection(eval($x));
+
+sub no_connection {
+
+ my (@routes, $r, $input, $output, %from, %to);
+
+ # create list of froms and tos where from and to are distinct
+ @routes = @_;
+ for $r (@routes) {
+ $input .= qq{['$r->[0]', '$r->[1]'], };
+ next if $r->[1] eq $r->[0];
+ $from{$r->[0]} = 1;
+ $to{$r->[1]} = 1;
+ }
+
+ # create list of froms and tos where from and to are distinct
+ $output = '';
+ for $r (@routes) {
+ next if $output =~ m|'$r->[1]'|;
+ $output .= qq['$r->[1]', ] unless defined $from{$r->[1]};
+ }
+
+ # edge case of ['X', 'X']
+ for $r (@routes) {
+ if ($r->[1] eq $r->[0] and not $from{$r->[1]}) {
+ $output .= qq['$r->[1]', ] unless $output =~ m|'$r->[1]'|;
+ }
+ }
+ say qq[\nInput: \@routes = (] . substr($input, 0, -2) . ')';
+ say qq[Output: ] . ($output ? substr($output, 0, -2) : 'none');
+}
diff --git a/challenge-285/peter-campbell-smith/perl/ch-2.pl b/challenge-285/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..f5b36cc871
--- /dev/null
+++ b/challenge-285/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-09-02
+use utf8; # Week 285 - task 2 - Making change
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+making_change(9);
+making_change(15);
+making_change(100);
+making_change(500);
+
+sub making_change {
+
+ my ($cents, $h, $q, $d, $n, $p, @sum, $count, $explain);
+
+ # initialise
+ $cents = $_[0];
+ $count = 0;
+ $explain = '';
+
+ # intelligently loop over all combs
+ H: for $h (0 .. 1e6) {
+ $sum[0] = $h * 50;
+ last H if $sum[0] > $cents;
+
+ Q: for $q (0 .. 1e6) {
+ $sum[1] = $sum[0] + $q * 25;
+ last Q if $sum[1] > $cents;
+
+ D: for $d (0 .. 1e6) {
+ $sum[2] = $sum[1] + $d * 10;
+ last D if $sum[2] > $cents;
+
+ N: for $n (0 .. 1e6) {
+ $sum[3] = $sum[2] + $n * 5;
+ last N if $sum[3] > $cents;
+
+ P: for $p (0 .. 1e6) {
+ $sum[4] = $sum[3] + $p;
+
+ # found a valid combination
+ if ($sum[4] == $cents) {
+ $count ++;
+ $explain .= qq[ $count: ${h}H + ${q}Q + ${d}D + ${n}N + ${p}P\n];
+ }
+ last P if $sum[4] >= $cents;
+ }
+ }
+ }
+ }
+ }
+
+ # output
+ say qq[\nInput: \$cents = $cents];
+ $explain = qq[\n] if $count > 300;
+ $explain =~ s| \+ 0.||gm;
+ $explain =~ s|0H \+ ||gm;
+ print qq[Output: $count\n$explain];
+}
+