aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2020-12-06 18:28:41 -0500
committerJaldhar H. Vyas <jaldhar@braincells.com>2020-12-06 18:28:41 -0500
commit2c1671d16eb88eb5d3d5dd8056aa7163ec78d7b3 (patch)
treec061e9bf8e578ba7b6df7d0881d4207eaa56bad8
parent09351d4e857b376c2affd5b13d90280ca72a9112 (diff)
downloadperlweeklychallenge-club-2c1671d16eb88eb5d3d5dd8056aa7163ec78d7b3.tar.gz
perlweeklychallenge-club-2c1671d16eb88eb5d3d5dd8056aa7163ec78d7b3.tar.bz2
perlweeklychallenge-club-2c1671d16eb88eb5d3d5dd8056aa7163ec78d7b3.zip
Challenge 89 by Jaldhar H. Vyas
-rw-r--r--challenge-089/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-089/jaldhar-h-vyas/perl/ch-1.pl39
-rwxr-xr-xchallenge-089/jaldhar-h-vyas/perl/ch-2.pl48
-rwxr-xr-xchallenge-089/jaldhar-h-vyas/raku/ch-1.sh1
-rwxr-xr-xchallenge-089/jaldhar-h-vyas/raku/ch-2.raku26
5 files changed, 115 insertions, 0 deletions
diff --git a/challenge-089/jaldhar-h-vyas/blog.txt b/challenge-089/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..9ad4d010e7
--- /dev/null
+++ b/challenge-089/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2020/12/perl_weekly_challenge_week_89.html
diff --git a/challenge-089/jaldhar-h-vyas/perl/ch-1.pl b/challenge-089/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..d516189870
--- /dev/null
+++ b/challenge-089/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub gcd {
+ my ($a, $b) = @_;
+
+ return 0 == $b ? $a : gcd($b, $a % $b);
+}
+
+my $N = $ARGV[0] // 1;
+my $sum = 0;
+
+for my $combo (combinations([1 .. $N], 2)) {
+ $sum += gcd($combo->[0], $combo->[1]);
+}
+
+say $sum; \ No newline at end of file
diff --git a/challenge-089/jaldhar-h-vyas/perl/ch-2.pl b/challenge-089/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..400b4f821f
--- /dev/null
+++ b/challenge-089/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub permute (&@) {
+ my $code = shift;
+ my @idx = 0..$#_;
+ while ( $code->(@_[@idx]) ) {
+ my $p = $#idx;
+ --$p while $idx[$p-1] > $idx[$p];
+ my $q = $p or return;
+ push @idx, reverse splice @idx, $p;
+ ++$q while $idx[$p-1] > $idx[$q];
+ @idx[$p-1,$q]=@idx[$q,$p-1];
+ }
+}
+
+sub sum {
+ my @arr = @_;
+ my $sum = 0;
+ for my $i (@arr) {
+ $sum += $i;
+ }
+
+ return $sum;
+}
+
+my @permutations;
+permute { push @permutations, \@_; } (1 .. 9);
+
+for my $permutation (@permutations) {
+ my @matrix = @{$permutation};
+
+ sum( @matrix[0, 1, 2] ) == 15 || next;
+ sum( @matrix[3, 4, 5] ) == 15 || next;
+ sum( @matrix[6, 7, 8] ) == 15 || next;
+ sum( @matrix[0, 3, 6] ) == 15 || next;
+ sum( @matrix[1, 4, 7] ) == 15 || next;
+ sum( @matrix[2, 5, 8] ) == 15 || next;
+ sum( @matrix[0, 4, 8] ) == 15 || next;
+ sum( @matrix[2, 4, 6] ) == 15 || next;
+
+ for my $row (0 .. 2) {
+ say q{[ }, (join q{ }, @matrix[$row * 3 .. $row * 3 + 2]), q{ ]};
+ }
+
+ last;
+}
diff --git a/challenge-089/jaldhar-h-vyas/raku/ch-1.sh b/challenge-089/jaldhar-h-vyas/raku/ch-1.sh
new file mode 100755
index 0000000000..56e87f4597
--- /dev/null
+++ b/challenge-089/jaldhar-h-vyas/raku/ch-1.sh
@@ -0,0 +1 @@
+raku -e 'say [+] (1 .. @*ARGS[0]).combinations(2).map({ [gcd] @_ });' $@ \ No newline at end of file
diff --git a/challenge-089/jaldhar-h-vyas/raku/ch-2.raku b/challenge-089/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..2f61aa130d
--- /dev/null
+++ b/challenge-089/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/raku
+
+sub MAIN() {
+
+ for (1 .. 9).permutations -> @permutation {
+
+ my @matrix;
+ for @permutation.batch(3) -> @row {
+ @matrix.push(@row);
+ }
+
+ ([+] @matrix[0]) == 15 || next;
+ ([+] @matrix[1]) == 15 || next;
+ ([+] @matrix[2]) == 15 || next;
+ ([+] @matrix[*;0]) == 15 || next;
+ ([+] @matrix[*;1]) == 15 || next;
+ ([+] @matrix[*;2]) == 15 || next;
+ ([+] (@matrix[0;0], @matrix[1;1], @matrix[2;2])) == 15 || next;
+ ([+] (@matrix[0;2], @matrix[1;1], @matrix[2;0])) == 15 || next;
+
+ for 0 ..^ @matrix.elems -> $row {
+ say q{[ }, @matrix[$row].join(q{ }), q{ ]};
+ }
+ last;
+ }
+}