aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-30 21:17:15 +0000
committerGitHub <noreply@github.com>2020-11-30 21:17:15 +0000
commit761f4c2def2cb05a65706d84febc06bc82ac251d (patch)
treedcbaeb21583887201422c2dd9d0723881333a018
parentfc028c6905ab96ea32a50b448ff5b7394d157aa1 (diff)
parentd67d47c09133a6f4c3ae40dd622eb3449685c374 (diff)
downloadperlweeklychallenge-club-761f4c2def2cb05a65706d84febc06bc82ac251d.tar.gz
perlweeklychallenge-club-761f4c2def2cb05a65706d84febc06bc82ac251d.tar.bz2
perlweeklychallenge-club-761f4c2def2cb05a65706d84febc06bc82ac251d.zip
Merge pull request #2888 from simongreen-net/master
sgreen solution to challenge 089
-rw-r--r--challenge-089/sgreen/README.md4
-rw-r--r--challenge-089/sgreen/blog.txt1
-rwxr-xr-xchallenge-089/sgreen/perl/ch-1.pl33
-rwxr-xr-xchallenge-089/sgreen/perl/ch-2.pl75
4 files changed, 111 insertions, 2 deletions
diff --git a/challenge-089/sgreen/README.md b/challenge-089/sgreen/README.md
index 99bc7be7ad..518f1c9558 100644
--- a/challenge-089/sgreen/README.md
+++ b/challenge-089/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 088
+# The Weekly Challenge 089
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-088-5c5f)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-089-f3)
diff --git a/challenge-089/sgreen/blog.txt b/challenge-089/sgreen/blog.txt
new file mode 100644
index 0000000000..49ffd73da4
--- /dev/null
+++ b/challenge-089/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-089-f3
diff --git a/challenge-089/sgreen/perl/ch-1.pl b/challenge-089/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..fb0e8e44f1
--- /dev/null
+++ b/challenge-089/sgreen/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use List::Util qw(min);
+
+sub _gcd {
+ my ( $m, $n ) = @_;
+ my $max = min( $m, $n );
+ for ( my $c = min( $m, $n ) ; $c >= 1 ; $c-- ) {
+ return $c unless $m % $c or $n % $c;
+ }
+}
+
+sub main {
+ my $N = shift;
+ my $sum = 0;
+
+ die "You must specify a value\n" unless $N;
+ die "The value is not a positive integer > 1\n" if $N =~ /[^0-9]/ or $N < 2;
+
+ for my $i ( 1 .. $N - 1 ) {
+ for my $j ( $i + 1 .. $N ) {
+ $sum += _gcd( $i, $j );
+ }
+ }
+
+ say $sum;
+}
+
+main(@ARGV);
diff --git a/challenge-089/sgreen/perl/ch-2.pl b/challenge-089/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..1fc920359e
--- /dev/null
+++ b/challenge-089/sgreen/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use List::Util qw(none);
+
+sub _combinations {
+ my $a = shift;
+
+ # Return all six ways that three numbers can be ordered
+ return (
+ [ $a->[0], $a->[1], $a->[2] ],
+ [ $a->[0], $a->[2], $a->[1] ],
+ [ $a->[1], $a->[0], $a->[2] ],
+ [ $a->[1], $a->[2], $a->[0] ],
+ [ $a->[2], $a->[0], $a->[1] ],
+ [ $a->[2], $a->[1], $a->[0] ],
+ );
+}
+
+sub main {
+
+ # Work out all ordered combinations of three number that total 15
+ my @rows = ();
+ for my $a ( 1 .. 4 ) {
+ for my $b ( $a + 1 .. 6 ) {
+ for my $c ( $b + 1 .. 9 ) {
+ push @rows, [ $a, $b, $c ] if $a + $b + $c == 15;
+ }
+ }
+ }
+
+ # Get all possible solutions using the above defined rows
+ my @solutions = ();
+ for my $first_row (@rows) {
+ my %first_seen = ( map { $_, 1 } @$first_row );
+ for my $second_row (@rows) {
+ if ( none { $first_seen{$_} } @$second_row ) {
+ # The third row is numbers not in the first two rows
+ my %second_seen = ( map { $_, 1 } @$second_row );
+ my $third_row =
+ [ grep { !$first_seen{$_} and !$second_seen{$_} }
+ ( 1 .. 9 ) ];
+ push @solutions, [ $first_row, $second_row, $third_row ];
+ }
+ }
+ }
+
+ # We now have all possible solutions, but don't know the order of
+ # values in each row. Time for some brute force work
+ foreach my $solution (@solutions) {
+ foreach my $first_row ( _combinations( $solution->[0] ) ) {
+ foreach my $second_row ( _combinations( $solution->[1] ) ) {
+ foreach my $third_row ( _combinations( $solution->[2] ) ) {
+ if ( $first_row->[0] + $second_row->[0] + $third_row->[0] == 15
+ and $first_row->[1] + $second_row->[1] + $third_row->[1] == 15
+ and $first_row->[2] + $second_row->[2] + $third_row->[2] == 15
+ and $first_row->[0] + $second_row->[1] + $third_row->[2] == 15
+ and $first_row->[2] + $second_row->[1] + $third_row->[0] == 15 )
+ {
+ # We have the solution
+ foreach ( $first_row, $second_row, $third_row ) {
+ say '[ ', join( ' ', @$_ ), ' ]';
+ }
+ return;
+ }
+ }
+ }
+ }
+ }
+}
+
+main();