aboutsummaryrefslogtreecommitdiff
path: root/challenge-052/dave-jacoby/perl
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2020-03-16 00:58:00 -0400
committerDave Jacoby <jacoby.david@gmail.com>2020-03-16 00:58:00 -0400
commit2a1e9dec22dc6ecd6e7b4a78b4732c6885b6d253 (patch)
tree9db6ed9fdb47eff4d757e97b624d56434b3cd603 /challenge-052/dave-jacoby/perl
parent0f685e69ee3456d0c9fdc072a626eb4cc466b4eb (diff)
downloadperlweeklychallenge-club-2a1e9dec22dc6ecd6e7b4a78b4732c6885b6d253.tar.gz
perlweeklychallenge-club-2a1e9dec22dc6ecd6e7b4a78b4732c6885b6d253.tar.bz2
perlweeklychallenge-club-2a1e9dec22dc6ecd6e7b4a78b4732c6885b6d253.zip
Don't watch my size; I'm dangerous.
Diffstat (limited to 'challenge-052/dave-jacoby/perl')
-rw-r--r--challenge-052/dave-jacoby/perl/ch-1.pl36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-052/dave-jacoby/perl/ch-1.pl b/challenge-052/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..4a9b1a5526
--- /dev/null
+++ b/challenge-052/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental::postderef experimental::signatures };
+
+# A number is called a stepping number if the adjacent digits
+# have a difference of 1. For example, 456 is a stepping number
+# but 129 is not.
+
+if ( scalar @ARGV > 1 ) {
+ my @nums = sort grep { $_ >= 100 && $_ <= 999 } map { int } @ARGV;
+ my @list = get_stepping_numbers( $nums[0], $nums[-1] );
+ say join ", ", @list;
+}
+else {
+ my @list = get_stepping_numbers( 100, 999 );
+ say join ", ", @list;
+}
+
+sub get_stepping_numbers ( $low, $high ) {
+ my @output;
+ for my $n ( $low .. $high ) {
+ my @n = split //, $n;
+ push @output, $n
+ if off_by_one( $n[0], $n[1] ) && off_by_one( $n[1], $n[2] );
+ }
+ return @output;
+}
+
+sub off_by_one ( $i, $j ) {
+ return 1 if $i == $j + 1;
+ return 1 if $i == $j - 1;
+ return 0;
+}