aboutsummaryrefslogtreecommitdiff
path: root/challenge-126
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-126')
-rwxr-xr-xchallenge-126/stuart-little/perl/ch-1.pl16
-rwxr-xr-xchallenge-126/stuart-little/perl/ch-2.pl31
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/perl/ch-1.pl b/challenge-126/stuart-little/perl/ch-1.pl
new file mode 100755
index 0000000000..c421696dcc
--- /dev/null
+++ b/challenge-126/stuart-little/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+use warnings;
+use v5.12;
+
+# run <script> <number>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+sub no1($nr) {
+ length($nr)==0 && return 0;
+ (substr($nr,0,1) eq '1') && return 9**(length($nr)-1);
+ return (int(substr($nr,0,1))-1) * 9**(length($nr)-1) + no1(substr($nr,1));
+}
+
+say(($ARGV[0] !~ m/1/) ? (no1($ARGV[0])) : (no1($ARGV[0])-1))
diff --git a/challenge-126/stuart-little/perl/ch-2.pl b/challenge-126/stuart-little/perl/ch-2.pl
new file mode 100755
index 0000000000..86a3d4d6f2
--- /dev/null
+++ b/challenge-126/stuart-little/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use warnings;
+use v5.12;
+
+# run <script>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+sub nbrs($mat, $i, $j) {
+ my @pairs = grep {$_->[0] >= 0 && $_->[0] < scalar @{$mat} && $_->[1] >= 0 && $_->[1] < scalar @{$mat->[0]} && ($_->[0] != $i || $_->[1] != $j)}
+ map {my $co = $_; my @a = map {[$i+$_,$j+$co]} (-1..1); @a}
+ (-1..1);
+ return \@pairs;
+}
+
+my @in = grep {scalar @{$_}} map {my @a = split /\s+/, $_; \@a} <DATA>;
+
+for my $i (0..(scalar @in)-1) {
+ for my $j (0..(scalar @{$in[0]})-1) {
+ print( (($in[$i]->[$j] eq 'x') ? ($in[$i]->[$j]) : (scalar grep { $in[$_->[0]]->[$_->[1]] eq 'x' } @{nbrs(\@in,$i,$j)})) . " ");
+ }
+ print("\n");
+}
+
+__DATA__
+x * * * x * x x x x
+* * * * * * * * * x
+* * * * x * x * x *
+* * * x x * * * * *
+x * * * x * * * * x