aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-126/james-smith/perl/ch-1.pl37
-rw-r--r--challenge-126/james-smith/perl/ch-2.pl36
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-126/james-smith/perl/ch-1.pl b/challenge-126/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..ac845eee9a
--- /dev/null
+++ b/challenge-126/james-smith/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @TESTS = (
+ [ 15, 8 ],
+ [ 25, 13 ],
+ [ 100, 80 ],
+ [ 1000, 728 ],
+ [ 1100, 728 ],
+ [ 1200, 728 ],
+ [ 1300, 728 ],
+ [ 2000, 729 ],
+ [ 2100, 809 ],
+ [ 2200, 810 ],
+ [ 3000, 1458 ],
+ [ 4000, 2187 ],
+ [ 5000, 2916 ],
+ [ 10000, 6560 ],
+ [ 100000, 59048 ],
+ [ 1000000, 531440 ],
+);
+
+is( get_no_one_count($_->[0]), $_->[1] ) foreach @TESTS;
+done_testing();
+
+sub get_no_one_count {
+ my $n = shift;
+ return scalar grep { ! m{1} } 2..$n;
+}
+
diff --git a/challenge-126/james-smith/perl/ch-2.pl b/challenge-126/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..72d66eaf61
--- /dev/null
+++ b/challenge-126/james-smith/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @grid = qw(x***x*xxxx *********x ****x*x*x* ***xx***** x***x****x);
+
+solve(@grid);
+
+sub solve {
+ my @res = ('');
+ ## Map input of strings into an array of 1s (bombs) + 0s (spaces)
+ my @in = map { [ map { $_ eq 'x' ? 1:0 } split //, $_ ] } @_;
+ ## Get max x value....
+ my($h,$w) = ($#_,-1 + length $_[0]);
+ foreach my $y (0..$h) {
+ $res[-1] .= $in[$y][$_] ? 'x' :
+ ( $y ? ( ($_?$in[$y-1][$_-1]:0)
+ +$in[$y-1][$_]
+ +($_<$w?$in[$y-1][$_+1]:0) ) : 0 ) +
+ ($_?$in[$y][$_-1]:0)
+ +$in[$y][$_]
+ +($_<$w?$in[$y][$_+1]:0) +
+ ( $y<$h ? ( ($_?$in[$y+1][$_-1]:0)
+ +$in[$y+1][$_]
+ +($_<$w?$in[$y+1][$_+1]:0) ) : 0 ) foreach 0..$w;
+ push @res, '';
+ }
+ print join "\n", @res;
+}
+