aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-069/colin-crain/perl/ch-1.pl59
1 files changed, 26 insertions, 33 deletions
diff --git a/challenge-069/colin-crain/perl/ch-1.pl b/challenge-069/colin-crain/perl/ch-1.pl
index c1a3c8d434..ef6497300d 100644
--- a/challenge-069/colin-crain/perl/ch-1.pl
+++ b/challenge-069/colin-crain/perl/ch-1.pl
@@ -28,65 +28,58 @@ use feature ":5.26";
## ## ## ## ## MAIN:
-my ($A, $B) = @ARGV; ## low, high bounds
-# ($A, $B) = (1000, 2000000000);
+my ($A, $B) = @ARGV; ## low, high bounds
+
# the order is the length of the half number that is modified and
-# joined to itself, with or without the pivot digit in the center.
-# Thus a given order will generate numbers up to 2n+1 places long, or
-# 10^(2n+1) The order is calculated to theoretically create numbers as
-# large as B. As this value scales by magnitude 100, this number can
-# be quite a bit larger, but will larger and we can guarantee none of
-# the next larger order will be required. It serves as an upper bound
+# joined to itself, with or without the pivot digit in the
+# center. Thus a given order will generate numbers up to 2n+1
+# places long, or numbers up to but less than 10^(2n+1) The order
+# is calculated to be large enough to create all numbers as large
+# as B. As the maximum of the range scales by magnitude 100, the
+# largest number created can still be quite a bit larger, but we
+# can guarantee it will be large enough and also that none of the
+# next larger order will be required. It serves as an upper bound
# to the calculation space.
my $order = int(length($B)/2);
my @list = (0, 1, 6, 8, 9);
my @center = (0, 1, 8);
-my @num = @list[1..@list-1]; ## remove leading 0
-my @stereo = grep { $_ >= $A && $_ <= $B } @center; ## single digit cases
+my @num = @list[1..@list-1]; ## remove leading 0
+my @stereo = @center; ## ensures trivial single digit cases
-OUTER: for (0..$order-1) {
- my @evens = my @odds = (); ## reset holding arrays
+for (0..$order-1) {
+ my @evens = my @odds = (); ## reset holding arrays
for my $left (@num) {
my $right = reverse($left =~ tr/69/96/r);
-
- my $even = "$left$right";
- if ($even > $B) {
- push @stereo, @evens, @odds; ## keeps things sorted
- last OUTER;
- }
- push @evens, $even unless $even < $A;
+ push @evens, "$left$right";
for my $center (@center) {
- my $odd = "$left$center$right";
- last if $odd > $B;
- next if $odd < $A;
- push @odds, $odd;
+ push @odds, "$left$center$right";
}
}
+ push @stereo, @evens, @odds; ## keeps things sorted
## add another digit to the working list
@num = map { my $c = $_; map "$c$_", @list } @num;
}
-# for ( @stereo ) {
-# next if $_ < $A;
-# last if $_ > $B;
-# say $_;
-# }
-
-say $_ for @stereo;
-
-
+## output within the specified range
+for ( @stereo ) {
+ next if $_ < $A;
+ last if $_ > $B;
+ say $_;
+}
## ## ## ## ## SUBS:
## included because I wanted to write a regex to find these numbers,
## but an impractical method in the end.
sub is_strobogrammatic {
- $_[0] =~ m/^([16890]*)[180]?((??{reverse($1=~tr[69][96]r)}))$/ ? 1 : 0
+ $_[0] =~ m/^([16890]+)[180]?((??{my $n=$1;$n=~tr[69][96];reverse($n)}))$/
+ ? "$_[0] is strobogrammatic"
+ : "$_[0] is not strobogrammatic"
}