From e108de310f13cb428d7657e1efedc8ad5f4ca150 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 14 Dec 2020 18:51:26 -0500 Subject: Challenge 91 --- challenge-091/dave-jacoby/blog.txt | 1 + challenge-091/dave-jacoby/perl/ch-1.pl | 33 +++++++++++++++++++++++++++++++++ challenge-091/dave-jacoby/perl/ch-2.pl | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 challenge-091/dave-jacoby/blog.txt create mode 100644 challenge-091/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-091/dave-jacoby/perl/ch-2.pl diff --git a/challenge-091/dave-jacoby/blog.txt b/challenge-091/dave-jacoby/blog.txt new file mode 100644 index 0000000000..da91abc05f --- /dev/null +++ b/challenge-091/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/12/14/perl-challenge-91-jumping-numbers-and-counting-numbers.html \ No newline at end of file diff --git a/challenge-091/dave-jacoby/perl/ch-1.pl b/challenge-091/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..75178dd1fe --- /dev/null +++ b/challenge-091/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +my @examples = qw{1122234 2333445 12345 90125 }; + +for my $example (@examples) { + my $output = count_number($example); + my $o2 = count_number2($example); + say ' input: ' . $example; + say ' output: ' . $output; + say 'output2: ' . $o2; + say ''; +} + +sub count_number( $input ) { + my $output = ''; + for my $i ( 0 .. 9 ) { + my $x = () = $input =~ /($i)/gmx; + $output .= $x . $i if $x; + } + return $output; +} + +sub count_number2( $input ) { + return join '', map { + my $x = () = $input =~ /($_)/g; + $x . $_; } + grep { $input =~ /$_/ } 0 .. 9; +} diff --git a/challenge-091/dave-jacoby/perl/ch-2.pl b/challenge-091/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..271c4345b1 --- /dev/null +++ b/challenge-091/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +say jump_game( 1, 2, 1, 2 ); +say jump_game( 2, 1, 1, 0, 2 ); +say jump_game( 1, 9, 9, 2 ); + +sub jump_game( @n ) { + say join ' ', ' ', @n; + my $i = 0; + while (1) { + if ( !defined $n[$i] ) { last } + if ( $n[$i] == 0 ) { last } + if ( defined $n[$i] && !defined $n[ $i + 1 ] ) { return 1 } + $i += $n[$i]; + } + return 0; +} -- cgit