aboutsummaryrefslogtreecommitdiff
path: root/challenge-117/perlboy1967
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2021-06-16 15:14:38 +0000
committerNiels van Dijke <perlboy@cpan.org>2021-06-16 15:14:38 +0000
commit6d5b2c4bdfdfb4ddb4f580fc5d184668cd701cec (patch)
tree0a56e5df6f16244527ddd359fbfbb036ebcef6ca /challenge-117/perlboy1967
parentda89030b24f8d0dd1e543375e8fb010f05b947e1 (diff)
downloadperlweeklychallenge-club-6d5b2c4bdfdfb4ddb4f580fc5d184668cd701cec.tar.gz
perlweeklychallenge-club-6d5b2c4bdfdfb4ddb4f580fc5d184668cd701cec.tar.bz2
perlweeklychallenge-club-6d5b2c4bdfdfb4ddb4f580fc5d184668cd701cec.zip
Task 1 & 2
Diffstat (limited to 'challenge-117/perlboy1967')
-rwxr-xr-xchallenge-117/perlboy1967/perl/ch-1.pl24
-rwxr-xr-xchallenge-117/perlboy1967/perl/ch-2.pl62
-rw-r--r--challenge-117/perlboy1967/perl/input.txt15
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-117/perlboy1967/perl/ch-1.pl b/challenge-117/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..0252ba108a
--- /dev/null
+++ b/challenge-117/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 117
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-117/#TASK1
+#
+# Task 1 - Missing Row(s)
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use File::Slurp;
+use List::MoreUtils qw(slide);
+
+printf "Missing row number(s) of file '%s' is/are '%s'\n", $ARGV[0], join(',',missingRows($ARGV[0]));
+
+
+sub missingRows {
+ my ($f) = @_;
+
+ return grep /\d/,slide{($a+1..$b-1)if($b-$a>1)}sort{$a<=>$b}map{chomp;s/^(\d+).*/$1/;$_}read_file($f);
+}
diff --git a/challenge-117/perlboy1967/perl/ch-2.pl b/challenge-117/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..8132e7ccfb
--- /dev/null
+++ b/challenge-117/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 117
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-117/#TASK2
+#
+# Task 2 - Find Possible Paths
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use List::Util qw(sum);
+
+use Test::More;
+use Test::Deep;
+use Data::Printer;
+
+# Prototype(s)
+sub findPossiblePaths($);
+
+my %tests = (
+ 1 => [qw(R LH)],
+ 2 => [qw(RR RLH LRH LLHH LHR LHLH)],
+ 3 => [qw(RRR RRLH RLRH RLLHH RLHR RLHLH LRRH LRLHH LRHR
+ LRHLH LLRHH LLLHHH LLHRH LLHLHH LLHHR LLHHLH
+ LHRR LHRLH LHLRH LHLLHH LHLHR LHLHLH)],
+);
+
+foreach my $test (sort keys %tests) {
+ cmp_deeply (findPossiblePaths($test),$tests{$test},"findPossiblePaths($test)");
+}
+
+done_testing;
+
+print "Now some bigger challenges...\n";
+for my $size (4 .. 10) {
+ printf "findPossiblePaths($size) = %d paths\n", scalar @{findPossiblePaths($size)};
+}
+
+
+sub findPossiblePaths($) {
+ my ($size) = @_;
+
+ my ($arRes,$arPath) = ([],[]);
+ _fpp($arRes,$arPath,$size,'T',0,0);
+ return $arRes;
+}
+
+sub _fpp {
+ my ($arRes,$arPath,$size,$direction,$level,$pos) = @_;
+
+ if ($level==$size and $pos==$level) {
+ push(@$arRes,join('',@$arPath));
+ return;
+ }
+
+ _fpp($arRes,[@$arPath,'R'],$size,'R',$level+1,$pos+1) if ($level<$size);
+ _fpp($arRes,[@$arPath,'L'],$size,'L',$level+1,$pos) if ($level<$size);
+ _fpp($arRes,[@$arPath,'H'],$size,'H',$level, $pos+1) if ($pos<$level);
+}
diff --git a/challenge-117/perlboy1967/perl/input.txt b/challenge-117/perlboy1967/perl/input.txt
new file mode 100644
index 0000000000..9822443667
--- /dev/null
+++ b/challenge-117/perlboy1967/perl/input.txt
@@ -0,0 +1,15 @@
+11, Line Eleven
+1, Line one
+9, Line Nine
+13, Line Thirteen
+2, Line two
+6, Line Six
+8, Line Eight
+10, Line Ten
+7, Line Seven
+4, Line Four
+14, Line Fourteen
+3, Line three
+15, Line Fifteen
+5, Line Five
+18, Line Eightteen (extra for more missing rows)