aboutsummaryrefslogtreecommitdiff
path: root/challenge-117
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-20 19:29:01 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-20 19:29:01 +0100
commit5cd90019ee016f15aff62c1e657eaf9600b6380b (patch)
treed51338b052c6a144bc410f05b93ab3e1b292cfa8 /challenge-117
parent1769cc8300770514d3bb92317d7f3368711e45fa (diff)
downloadperlweeklychallenge-club-5cd90019ee016f15aff62c1e657eaf9600b6380b.tar.gz
perlweeklychallenge-club-5cd90019ee016f15aff62c1e657eaf9600b6380b.tar.bz2
perlweeklychallenge-club-5cd90019ee016f15aff62c1e657eaf9600b6380b.zip
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-117')
-rw-r--r--challenge-117/pete-houston/perl/ch-1.pl24
-rw-r--r--challenge-117/pete-houston/perl/ch-2.pl56
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-117/pete-houston/perl/ch-1.pl b/challenge-117/pete-houston/perl/ch-1.pl
new file mode 100644
index 0000000000..420c093942
--- /dev/null
+++ b/challenge-117/pete-houston/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 11701.pl
+#
+# USAGE: ./11701.pl INFILE
+#
+# DESCRIPTION: Find the missing numbered row out of 15 in INFILE
+#
+# REQUIREMENTS: Path::Tiny
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 14/06/21
+#===============================================================================
+
+use strict;
+use warnings;
+use Path::Tiny;
+
+my %rows = map { $_ => 1 } 1..15;
+delete $rows{$_} for map { s/,.*$//; $_ } path (shift)->lines ({ chomp => 1});
+my ($missing) = keys %rows;
+print "Row $missing is missing\n";
diff --git a/challenge-117/pete-houston/perl/ch-2.pl b/challenge-117/pete-houston/perl/ch-2.pl
new file mode 100644
index 0000000000..6bd2272217
--- /dev/null
+++ b/challenge-117/pete-houston/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+#===============================================================================
+#
+# FILE: 11702.pl
+#
+# USAGE: ./11702.pl N
+#
+# DESCRIPTION: For an equilateral triangle of sides N units, find all
+# possible paths from the apex to lower right vertex travelling
+# down-left, down-right and/or flat-right only.
+#
+# NOTES: A size of 10 takes under 2s on my machine. YMMV.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 14/06/21
+#===============================================================================
+
+use strict;
+use warnings;
+
+my ($n) = (shift =~ /([0-9]+)/);
+die "Bad argument - must be a small natural number.\n" unless $n && $n < 21;
+
+my %subs; # memoize the end paths
+my @paths = walk ($n, 0);
+print "@paths\n";
+
+# The co-ordinate system used here is that the first dimension is how
+# far from the floor of the triangle we are (think y axis), the second
+# is how far from the right slope we are (negative x axis but angled).
+# Bottom-right vertex is (0,0), bottom-left is (0, $n), top is ($n, 0).
+# Just pass the co-ordinates to walk() and you'll get back an array of
+# all the possible paths from there to (0,0).
+
+sub walk {
+ my ($l, $x) = @_;
+ return @{$subs{$l}{$x}} if exists $subs{$l}{$x};
+
+ my @paths;
+ if ($l) {
+ # Not at the bottom yet so can go left or right
+ push @paths, map { 'L' . $_ } walk ($l - 1, $x + 1);
+ push @paths, map { 'R' . $_ } walk ($l - 1, $x);
+ }
+ if ($x) {
+ # Not at right edge yet, so can go horizontally right
+ push @paths, map { 'H' . $_ } walk ($l, $x - 1);
+ }
+
+ # If at the bottom right already, no paths from here
+ @paths = ('') unless $l || $x;
+
+ $subs{$l}{$x} = \@paths;
+ return @paths;
+}