aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-10-11 18:19:44 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-10-11 18:19:44 -0400
commite91720d5c5c6b1c4bbe0b925dd686baaeec485a5 (patch)
tree64f2b3d6bf03483ad6ed141c7f316bf0d71904dc
parent5f01f0a38aa1e5a76c4262de8c7c6e1977c594ea (diff)
downloadperlweeklychallenge-club-e91720d5c5c6b1c4bbe0b925dd686baaeec485a5.tar.gz
perlweeklychallenge-club-e91720d5c5c6b1c4bbe0b925dd686baaeec485a5.tar.bz2
perlweeklychallenge-club-e91720d5c5c6b1c4bbe0b925dd686baaeec485a5.zip
I DID IT
-rw-r--r--challenge-134/dave-jacoby/blog.txt1
-rw-r--r--challenge-134/dave-jacoby/perl/ch-1.pl74
-rw-r--r--challenge-134/dave-jacoby/perl/ch-2.pl74
3 files changed, 149 insertions, 0 deletions
diff --git a/challenge-134/dave-jacoby/blog.txt b/challenge-134/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..796242873e
--- /dev/null
+++ b/challenge-134/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/10/11/there-are-wrong-ways-to-skin-a-cat-the-weekly-challenge-134.html
diff --git a/challenge-134/dave-jacoby/perl/ch-1.pl b/challenge-134/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..77657816bd
--- /dev/null
+++ b/challenge-134/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say state postderef signatures };
+no warnings qw{ experimental };
+
+use Algorithm::Permute;
+
+my @x = pandigital_1();
+my @y = pandigital_2();
+my @z = pandigital_3();
+
+my @headers = qw{I PANDIGITAL1 PANDIGITAL2 PANDIGITAL3};
+say join "\t", @headers;
+say join "\t", map {s/./-/gmix;$_} @headers;
+for my $i ( 0 .. 4 ) {
+ say join "\t", $i, $x[$i], $y[$i], $z[$i];
+}
+
+
+sub pandigital_1 {
+ my @output;
+ my @nums = ( 0, 2 .. 9 );
+ my $p = Algorithm::Permute->new( \@nums );
+ while ( my @res = $p->next ) {
+ my $n = join '', 1, @res;
+ push @output, $n;
+ }
+ @output = sort { $a <=> $b } @output;
+ return @output[ 0 .. 4 ];
+}
+
+sub pandigital_2 {
+ my $output = [];
+ my $state = [1];
+ _pandigital_2( $output, $state );
+ my @output = $output->@*;
+ return @output[ 0 .. 4 ];
+}
+
+sub _pandigital_2 ( $output, $state ) {
+ my %state = map { $_ => 1 } $state->@*;
+ my @digits = grep { !$state{$_} } 0 .. 9;
+ if ( scalar $output->@* > 5 ) { return }
+ if ( scalar $state->@* == 10 ) {
+ my $pandigit = join '', $state->@*;
+ push $output->@*, $pandigit;
+ return;
+ }
+ for my $i (@digits) {
+ my $newstate->@* = $state->@*;
+ push $newstate->@*, $i;
+ _pandigital_2( $output, $newstate );
+ }
+ return;
+}
+
+sub pandigital_3 {
+ my @output;
+ my $i = 1023456789;
+ while ( scalar @output < 5 ) {
+ push @output, $i if is_pandigital($i);
+ $i++;
+ }
+ return @output[ 0 .. 4 ];
+}
+
+sub is_pandigital ( $n ) {
+ for my $i ( 0 .. 9 ) {
+ return 0 unless $n =~ /$i/;
+ }
+ return 1;
+}
diff --git a/challenge-134/dave-jacoby/perl/ch-2.pl b/challenge-134/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..4bec861482
--- /dev/null
+++ b/challenge-134/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures };
+no warnings qw{ experimental };
+
+use Carp;
+use Getopt::Long;
+use List::Util qw{ uniq };
+
+my $x = 3;
+my $y = $x;
+
+GetOptions(
+ 'x=i' => \$x,
+ 'y=i' => \$y,
+);
+
+croak 'X not positive' unless $x > 0;
+croak 'Y not positive' unless $x > 0;
+croak 'X not integer' unless $x == int $x;
+croak 'Y not integer' unless $y == int $y;
+
+make_table( $x, $y );
+
+sub make_table ( $x, $y ) {
+ my @headers = make_line( 'x', '|', 1 .. $y );
+ my $headers = join ' ', @headers;
+ my $line = $headers;
+ $line =~ s/\|/+/gmix;
+ $line =~ s/[\w\s]/-/gmix;
+
+ say qq{\$x = $x , \$y = $y };
+ say '';
+ say $headers;
+ say $line;
+ my $matrix = make_matrix( $x, $y );
+ my @dt = uniq sort {$a<=>$b} flatten_matrix($matrix);
+ my $dt = join ', ', @dt;
+ my $count = scalar @dt;
+
+ my $c = 0;
+ for my $i ( $matrix->@* ) {
+ $c++;
+ my $line = make_line( $c, '|', $i->@* );
+ say $line;
+ }
+ say '';
+ say qq{Distinct Terms: $dt};
+ say qq{Count: $count};
+}
+
+sub make_line ( @array ) {
+ my @headers = ( map { sprintf '%3s', $_ } @array );
+ return join ' ', @headers;
+}
+
+sub make_matrix ( $x, $y ) {
+ my $matrix;
+ for my $i ( 0 .. $x - 1 ) {
+ my $ii = $i + 1;
+ for my $j ( 0 .. $y - 1 ) {
+ my $jj = $j + 1;
+ my $tt = $ii * $jj;
+ $matrix->[$i][$j] = $tt;
+ }
+ }
+ return $matrix;
+}
+
+sub flatten_matrix ( $matrix ) {
+ return map { $_->@* } $matrix->@*;
+} \ No newline at end of file