aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2021-06-05 00:08:16 -0500
committerLuis Mochan <mochan@fis.unam.mx>2021-06-05 00:08:16 -0500
commitaa64a9da8e325855e9d3e72bd571b16b90bbf78a (patch)
tree3e90f67e2ce7b6d532f71aa665c5ffe99795c359
parentbce31592a7b0214072fc2fbf9a0aa26c3a4b5be4 (diff)
downloadperlweeklychallenge-club-aa64a9da8e325855e9d3e72bd571b16b90bbf78a.tar.gz
perlweeklychallenge-club-aa64a9da8e325855e9d3e72bd571b16b90bbf78a.tar.bz2
perlweeklychallenge-club-aa64a9da8e325855e9d3e72bd571b16b90bbf78a.zip
Add comment
-rwxr-xr-xchallenge-115/wlmb/perl/ch-1.pl4
-rwxr-xr-xchallenge-115/wlmb/perl/ch-1a.pl6
2 files changed, 7 insertions, 3 deletions
diff --git a/challenge-115/wlmb/perl/ch-1.pl b/challenge-115/wlmb/perl/ch-1.pl
index a2accad0fd..7c3be5eee7 100755
--- a/challenge-115/wlmb/perl/ch-1.pl
+++ b/challenge-115/wlmb/perl/ch-1.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
# Perl weekly challenge 115
-# Task 1: String Chain
+# Task 1: String Chain. Connectivity matrix solution.
#
# See https://wlmb.github.io/2021/06/01/PWC115/#task-1-string-chain
use strict;
@@ -14,7 +14,7 @@ die "Usage ./ch-1.pl string1 [string2...]" unless @strings;
my $C=zeroes(long,scalar(@strings), scalar(@strings)); #connectivity matrix
map {my $f=$_;map {$C->slice("$f,$_").=follows($f, $_)}
(0..@strings-1)}(0..@strings-1);
-my $R=reduce {map{$_->diagonal(0,1).=0}($a,$b); $b x $a;}
+my $R=reduce {map{$_->diagonal(0,1).=0}($a, $b); $b x $a;}
($C) x @strings;
say "Input: ", join " ", @strings;
say "Output: ", all($R->diagonal(0,1)>0);
diff --git a/challenge-115/wlmb/perl/ch-1a.pl b/challenge-115/wlmb/perl/ch-1a.pl
index a1edad7c3e..afc99d62a4 100755
--- a/challenge-115/wlmb/perl/ch-1a.pl
+++ b/challenge-115/wlmb/perl/ch-1a.pl
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
# Perl weekly challenge 115
-# Task 1: String Chain
+# Task 1: String Chain. Build the circle.
#
# See https://wlmb.github.io/2021/06/01/PWC115/#task-1-string-chain
use strict;
@@ -23,21 +23,25 @@ say "Input: ", join " ", @strings;
my $path=first {follows($_->[-1], $_->[0])} @paths;
say "Output: ", defined $path?1:0;
say "Path: ", join "-", @$path if defined $path;
+
sub follows {
my ($from, $to)=@_;
return substr($from,-1,1) eq substr($to,0,1);
}
+
sub grow {
my @paths=@_;
my @new;
push @new, grow_one($_) for @paths;
return @new;
}
+
sub grow_one {
my $path=shift;
my @new=grep {defined $_} map {add_to($path, $_)} @{$followers{$path->[-1]}};
return @new;
}
+
sub add_to {
my ($path, $string)=@_;
return if any {$_ eq $string} @$path; # don't add duplicates