aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrtastic <brtastic.dev@gmail.com>2020-06-08 18:51:26 +0200
committerbrtastic <brtastic.dev@gmail.com>2020-06-08 18:51:26 +0200
commit5499df0048c6445063b1c6733ebf3e33ef742a87 (patch)
treee1494ce8a40616d1b501d409c84567c225aef4d0
parentf2c3c84295ef3b02c765dcf5b72777892c30b9da (diff)
downloadperlweeklychallenge-club-5499df0048c6445063b1c6733ebf3e33ef742a87.tar.gz
perlweeklychallenge-club-5499df0048c6445063b1c6733ebf3e33ef742a87.tar.bz2
perlweeklychallenge-club-5499df0048c6445063b1c6733ebf3e33ef742a87.zip
Challenge 64 solution
-rw-r--r--challenge-064/brtastic/blog.txt1
-rw-r--r--challenge-064/brtastic/perl/ch-1.pl59
-rw-r--r--challenge-064/brtastic/perl/ch-2.pl62
3 files changed, 122 insertions, 0 deletions
diff --git a/challenge-064/brtastic/blog.txt b/challenge-064/brtastic/blog.txt
new file mode 100644
index 0000000000..a0109a3783
--- /dev/null
+++ b/challenge-064/brtastic/blog.txt
@@ -0,0 +1 @@
+https://brtastic.xyz/blog/article/perl-weekly-64
diff --git a/challenge-064/brtastic/perl/ch-1.pl b/challenge-064/brtastic/perl/ch-1.pl
new file mode 100644
index 0000000000..86a05fdb36
--- /dev/null
+++ b/challenge-064/brtastic/perl/ch-1.pl
@@ -0,0 +1,59 @@
+use v5.24;
+use warnings;
+use List::Util qw(min);
+
+sub find_path
+{
+ my ($matrix) = @_;
+
+ my @pathfinder = reverse map { [reverse $_->@*] } $matrix->@*;
+ foreach my $path_x (keys @pathfinder) {
+ foreach my $path_y (keys $pathfinder[$path_x]->@*) {
+ my @alternatives;
+
+ push @alternatives, $pathfinder[$path_x][$path_y - 1]
+ if $path_y - 1 >= 0;
+ push @alternatives, $pathfinder[$path_x - 1][$path_y]
+ if $path_x - 1 >= 0;
+
+ if (@alternatives > 0) {
+ $pathfinder[$path_x][$path_y] += min @alternatives;
+ }
+ }
+ }
+
+ return $pathfinder[-1][-1];
+}
+
+use Test::More;
+
+my @data = (
+ [
+ [
+ [qw( 1 2 3 )],
+ [qw( 4 5 6 )],
+ [qw( 7 8 9 )],
+ ], 21
+ ],
+ [
+ [
+ [qw( 1 10 10 )],
+ [qw( 1 1 1 )],
+ [qw( 10 10 1 )],
+ ], 5
+ ],
+ [
+ [
+ [qw( 1 10 10 )],
+ [qw( 1 10 1 )],
+ [qw( 10 10 1 )],
+ ], 14
+ ]
+);
+
+for (@data) {
+ my ($matrix, $output) = @$_;
+ is find_path($matrix), $output, "path ok";
+}
+
+done_testing;
diff --git a/challenge-064/brtastic/perl/ch-2.pl b/challenge-064/brtastic/perl/ch-2.pl
new file mode 100644
index 0000000000..9561d99214
--- /dev/null
+++ b/challenge-064/brtastic/perl/ch-2.pl
@@ -0,0 +1,62 @@
+use v5.24;
+use warnings;
+
+sub match_words
+{
+ my ($string, @words) = @_;
+ @words = sort { length $b <=> length $a } @words;
+
+ my @found;
+
+ WORD_LOOP:
+ while (length $string > 0) {
+ for my $word (@words) {
+
+ if (substr($string, 0, length $word) eq $word) {
+ $string = substr $string, length $word;
+ push @found, $word;
+ next WORD_LOOP;
+ }
+ }
+
+ return 0;
+ }
+
+ return
+ join ", ",
+ map { '"' . $_ . '"' }
+ @found
+ ;
+}
+
+use Test::More;
+
+my @data = (
+ [
+ "perlweeklychallenge",
+ [qw(weekly challenge perl)],
+ '"perl", "weekly", "challenge"',
+ ],
+ [
+ "perlandraku",
+ [qw(python ruby haskell)],
+ 0,
+ ],
+ [
+ "perlandraku",
+ [qw(perl raku)],
+ 0,
+ ],
+ [
+ "perlishperl",
+ [qw(perl perlish)],
+ '"perlish", "perl"',
+ ],
+);
+
+for (@data) {
+ my ($string, $words, $output) = @$_;
+ is match_words($string, @$words), $output, "string match ok";
+}
+
+done_testing;