aboutsummaryrefslogtreecommitdiff
path: root/challenge-109
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-25 01:16:06 +0100
committerGitHub <noreply@github.com>2021-04-25 01:16:06 +0100
commitcf2348f20f35017e912c8deff65fb8687ac6be62 (patch)
tree6240e98b5ea865b87bcdb907ae10c0109b4a06fb /challenge-109
parent9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15 (diff)
parent1fe3699c0ef3f40a145bb666cbff64a59d3aaba6 (diff)
downloadperlweeklychallenge-club-cf2348f20f35017e912c8deff65fb8687ac6be62.tar.gz
perlweeklychallenge-club-cf2348f20f35017e912c8deff65fb8687ac6be62.tar.bz2
perlweeklychallenge-club-cf2348f20f35017e912c8deff65fb8687ac6be62.zip
Merge pull request #3942 from brtastic/challenge-109
Challenge 109 solutions by brtastic
Diffstat (limited to 'challenge-109')
-rw-r--r--challenge-109/brtastic/perl/ch-1.pl9
-rw-r--r--challenge-109/brtastic/perl/ch-2.pl52
-rw-r--r--challenge-109/brtastic/php/ch-1.php8
-rw-r--r--challenge-109/brtastic/php/ch-2.php75
-rw-r--r--challenge-109/brtastic/raku/ch-1.raku7
-rw-r--r--challenge-109/brtastic/raku/ch-2.raku16
6 files changed, 167 insertions, 0 deletions
diff --git a/challenge-109/brtastic/perl/ch-1.pl b/challenge-109/brtastic/perl/ch-1.pl
new file mode 100644
index 0000000000..5e39abfbe3
--- /dev/null
+++ b/challenge-109/brtastic/perl/ch-1.pl
@@ -0,0 +1,9 @@
+use v5.30;
+use warnings;
+use List::Util qw(sum0);
+
+for my $num (1 .. 20) {
+ say sum0 grep {
+ $num % $_ == 0
+ } 2 .. int($num / 2);
+}
diff --git a/challenge-109/brtastic/perl/ch-2.pl b/challenge-109/brtastic/perl/ch-2.pl
new file mode 100644
index 0000000000..d777e956b5
--- /dev/null
+++ b/challenge-109/brtastic/perl/ch-2.pl
@@ -0,0 +1,52 @@
+use v5.30;
+use warnings;
+use List::Util qw(all sum0);
+
+use constant EL_COUNT => 7;
+
+sub permute
+{
+ my (@what) = @_;
+
+ return [@what] if @what == 1;
+
+ my @options;
+
+ for my $el (@what) {
+ my $seen = 0;
+ push @options, map {
+ [$el, @$_]
+ } permute(grep {
+ $_ != $el || $seen++
+ } @what);
+ }
+
+ return @options;
+}
+
+sub four_squares
+{
+ my (@input) = @_;
+ my @results;
+
+ return @results if @input != EL_COUNT;
+
+ for my $case (permute @input) {
+ my @real_case = (0, @$case, 0);
+ my @summed_groups = map {
+ sum0 map { $real_case[$_] } $_ .. $_ + 2
+ } grep {
+ $_ % 2 == 0 && $_ <= @real_case - 2
+ } keys @real_case;
+
+ my $letter = 'a';
+ push @results, {map { $letter++, $_ } @$case}
+ if all { $_ == @summed_groups[0] } @summed_groups;
+ }
+
+ return @results;
+}
+
+use Data::Dumper;
+say Dumper([four_squares 1 .. 7]);
+
diff --git a/challenge-109/brtastic/php/ch-1.php b/challenge-109/brtastic/php/ch-1.php
new file mode 100644
index 0000000000..cc5c5e6de3
--- /dev/null
+++ b/challenge-109/brtastic/php/ch-1.php
@@ -0,0 +1,8 @@
+<?php
+
+foreach(range(1, 20) as $num) {
+ echo array_sum(array_filter(
+ $num >= 4 ? range(2, floor($num / 2)) : [],
+ fn ($divisor) => $num % $divisor == 0
+ )) . "\n";
+}
diff --git a/challenge-109/brtastic/php/ch-2.php b/challenge-109/brtastic/php/ch-2.php
new file mode 100644
index 0000000000..304967ffae
--- /dev/null
+++ b/challenge-109/brtastic/php/ch-2.php
@@ -0,0 +1,75 @@
+<?php
+
+const EL_COUNT = 7;
+
+function permute($what)
+{
+ if (count($what) == 1) {
+ return [$what];
+ }
+
+ $options = [];
+
+ foreach($what as $el) {
+ $seen = 0;
+ array_push($options, ...array_map(
+ fn ($arr) => [$el, ...$arr],
+ permute(
+ array_filter(
+ $what,
+ fn ($subel)
+ => $subel != $el || $seen++
+ )
+ )
+ ));
+ }
+
+ return $options;
+}
+
+function four_squares($input)
+{
+ $results = [];
+
+ if (count($input) != EL_COUNT) {
+ return $results;
+ }
+
+ foreach (permute($input) as $case) {
+ $real_case = [0, ...$case, 0];
+ $summed_groups = array_map(
+ fn ($el)
+ => array_sum(
+ array_map(
+ fn ($subel)
+ => $real_case[$subel],
+ range($el, $el + 2))
+ ),
+ array_filter(
+ array_keys($real_case),
+ fn ($el)
+ => $el % 2 == 0 && $el <= count($real_case) - 2
+ )
+ );
+
+ $matching_first = array_filter(
+ $summed_groups,
+ fn ($el)
+ => $el == $summed_groups[0]
+ );
+
+ if (count($matching_first) == count($summed_groups)) {
+ $case_results = [];
+ for ($letter = 'a'; $letter != 'h'; ++$letter) {
+ $case_results[$letter] = array_shift($case);
+ }
+
+ $results[] = $case_results;
+ }
+ }
+
+ return $results;
+}
+
+var_dump(four_squares(range(1, 7)));
+
diff --git a/challenge-109/brtastic/raku/ch-1.raku b/challenge-109/brtastic/raku/ch-1.raku
new file mode 100644
index 0000000000..fab08a6c80
--- /dev/null
+++ b/challenge-109/brtastic/raku/ch-1.raku
@@ -0,0 +1,7 @@
+for 1 .. 20 -> $num {
+ say [+] gather {
+ for 1 ^.. ($num / 2).truncate -> $divisor {
+ take $divisor if $num %% $divisor;
+ }
+ }
+}
diff --git a/challenge-109/brtastic/raku/ch-2.raku b/challenge-109/brtastic/raku/ch-2.raku
new file mode 100644
index 0000000000..2495669858
--- /dev/null
+++ b/challenge-109/brtastic/raku/ch-2.raku
@@ -0,0 +1,16 @@
+constant $el-count = 7;
+
+sub four-squares(@input where .elems == $el-count --> Array[Map])
+{
+ my Map @results;
+ for @input.permutations -> @case {
+ my @sum_groups = (0, |@case, 0).rotor(3 => -1);
+
+ @results.push: %(flat zip 'a' .. 'g', @case)
+ if [==] @sum_groups.map: { [+] $_ };
+ }
+
+ return @results;
+}
+
+say four-squares 1 .. 7;