diff options
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-2.pl | 45 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-067/simon-miner/perl/ch-1.pl b/challenge-067/simon-miner/perl/ch-1.pl new file mode 100755 index 0000000000..716e8df984 --- /dev/null +++ b/challenge-067/simon-miner/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; + +my ( $m, $n ) = @ARGV; +die "Max number $m is must be an integer.\n" if $m =~ m/\D/; +die "Combination length $n is must be an integer.\n" if $n =~ m/\D/; + +my @combinations = get_combinations( 1, $m, $n - 1 ); +print join( "\n", @combinations ) . "\n"; + +sub get_combinations { + my ( $min, $m, $n ) = @_; + + my @combinations = (); + if ( $n ) { + for my $i ( $min .. ( $m - 1 )) { + for my $combo ( get_combinations( $i + 1, $m, $n - 1 ) ) { + push( @combinations, $i . $combo ); + } + } + } else { + @combinations = ( $min .. $m ); + } + + return @combinations; +} diff --git a/challenge-067/simon-miner/perl/ch-2.pl b/challenge-067/simon-miner/perl/ch-2.pl new file mode 100755 index 0000000000..b3856a6dc8 --- /dev/null +++ b/challenge-067/simon-miner/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; + +my @letters = ( + ['+'], # Need some way to track 0 to avoid colliding strings. + [ '_', ',', '@' ], + [qw( A B C )], + [qw( D E F )], + [qw( G H I )], + [qw( J K L )], + [qw( M N O )], + [qw( P Q R S )], + [qw( T U V )], + [qw( W X Y Z )], +); + +my $S = shift( @ARGV ); +die "Stringcan only contain digits 0-9" if $S =~ m/[\D]/; + +my @letter_strings = get_letter_strings( $S ); +print join( "\n", @letter_strings ) . "\n"; + +sub get_letter_strings { + my ( $S ) = @_; + return () unless $S; + + $S =~ s/^(\d)// && ( my $digit = $1 ); + + my @letter_strings = (); + my @letters = @{ $letters[$digit] }; + if ( $S ) { + for my $letter ( @letters ) { + for my $letter_string ( get_letter_strings( $S ) ) { + push( @letter_strings, $letter . $letter_string ); + } + } + } + else { + @letter_strings = @letters; + } + + return @letter_strings; +} |
