diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2020-11-30 23:55:58 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2020-11-30 23:55:58 -0500 |
| commit | 553eac2662431858e609d776ee0d6af39b4c86c0 (patch) | |
| tree | 58b51d6c4f4095295b481a1b17a13e76a5617dc4 | |
| parent | c860104f34a4807557a487def7ccbf9815e724b7 (diff) | |
| download | perlweeklychallenge-club-553eac2662431858e609d776ee0d6af39b4c86c0.tar.gz perlweeklychallenge-club-553eac2662431858e609d776ee0d6af39b4c86c0.tar.bz2 perlweeklychallenge-club-553eac2662431858e609d776ee0d6af39b4c86c0.zip | |
Challege 89
| -rw-r--r-- | challenge-089/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-089/dave-jacoby/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-089/dave-jacoby/perl/ch-2.pl | 56 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-089/dave-jacoby/blog.txt b/challenge-089/dave-jacoby/blog.txt new file mode 100644 index 0000000000..02f5f4d165 --- /dev/null +++ b/challenge-089/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/11/30/perl-challenge-89-and-the-return-of-the-son-of-overkill.html
\ No newline at end of file diff --git a/challenge-089/dave-jacoby/perl/ch-1.pl b/challenge-089/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..058d9d0b06 --- /dev/null +++ b/challenge-089/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +use Carp; +use Getopt::Long; +use List::Util qw{ max }; +use List::Compare; + +my $n = 3; +GetOptions( 'number=i' => \$n ); +croak 'Negative Number' if $n < 1; +my $o = gcd($n); +say <<"END"; + INPUT: $n + OUTPUT: $o +END + +sub gcd( $n ) { + my $output = 0; + for my $i ( 1 .. $n - 1 ) { + for my $j ( $i + 1 .. $n ) { + my $di->@* = get_divisors($i); + my $dj->@* = get_divisors($j); + my $dc = List::Compare->new( $di, $dj ); + my @d = $dc->get_intersection; + my $g = max @d; + $output += $g; + } + } + return $output; +} + +sub get_divisors ( $n ) { + my @div; + for my $i ( 1 .. $n ) { push @div, $i if $n % $i == 0; } + return @div; +} diff --git a/challenge-089/dave-jacoby/perl/ch-2.pl b/challenge-089/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..8748e3dd28 --- /dev/null +++ b/challenge-089/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say signatures state }; +no warnings qw{ experimental }; + +my $numbers = [ 1 .. 9 ]; +my $array; + +recurse_magic_box( $numbers, $array ); + +sub recurse_magic_box ( $numbers, $array ) { + + # numbers is the list of allowable numbers + for my $n (@$numbers) { + push @$array, $n; + if ( check_magic_box($array) ) { + recurse_magic_box( $numbers, $array ); + } + pop @$array; + } +} + +sub check_magic_box ( $array ) { + for my $n (@$array) { + my $c = scalar grep { m{$n} } @$array; + return 0 if $c > 1; + } + + if ( scalar @$array == 9 ) { + my $sum = 15; + my $checks = [ + [ 0, 1, 2 ], # first row + [ 3, 4, 5 ], # second row + [ 6, 7, 8 ], # third row + [ 0, 3, 6 ], # first col + [ 1, 4, 7 ], # second col + [ 2, 5, 8 ], # third col + [ 0, 4, 8 ], # diagonal from top right + [ 6, 4, 2 ], # diagonal from bottom right + ]; + for my $check (@$checks) { + my $s = 0; + for my $p (@$check) { + $s += $array->[$p]; + } + return 0 if $s != $sum; + } + say "\t" . join ' ', @$array[ 0 .. 2 ]; + say "\t" . join ' ', @$array[ 3 .. 5 ]; + say "\t" . join ' ', @$array[ 6 .. 8 ]; + say ''; + } + return 1; +} |
