aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-31 12:38:35 +0100
committerGitHub <noreply@github.com>2020-08-31 12:38:35 +0100
commit00a2ade9ce739e6b9ab78d09afdef757aaaa1933 (patch)
tree35a2a10a56927cb18bc3a18d7927ae4034a7bded
parent2e699cb858dae073cdb50d310c86bfb60609b714 (diff)
parentd092c7c4be92d262037e1297ea08ce83dd812689 (diff)
downloadperlweeklychallenge-club-00a2ade9ce739e6b9ab78d09afdef757aaaa1933.tar.gz
perlweeklychallenge-club-00a2ade9ce739e6b9ab78d09afdef757aaaa1933.tar.bz2
perlweeklychallenge-club-00a2ade9ce739e6b9ab78d09afdef757aaaa1933.zip
Merge pull request #2180 from andemark/branch-for-challenge-076
Initial Week 76 Solutions
-rw-r--r--challenge-076/mark-anderson/raku/ch-1.raku60
-rw-r--r--challenge-076/mark-anderson/raku/ch-2.raku158
2 files changed, 218 insertions, 0 deletions
diff --git a/challenge-076/mark-anderson/raku/ch-1.raku b/challenge-076/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..a458ab46a5
--- /dev/null
+++ b/challenge-076/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,60 @@
+# with help from https://www.geeksforgeeks.org/minimum-number-of-primes-required-such-that-their-sum-is-equal-to-n/
+
+=begin usage
+
+Usage: raku ch-1.raku 27
+
+Output:
+
+2, 2, 23
+3, 5, 19
+3, 7, 17
+3, 11, 13
+5, 11, 11
+5, 5, 17
+7, 7, 13
+
+=end usage
+
+sub MAIN(UInt $N where $N > 1) {
+ given $N {
+ when .is-prime {
+ say $N;
+ }
+ when * %% 2 {
+ .say for min-primes($N, 2);
+ }
+ default {
+ if ($N-2).is-prime {
+ .say for min-primes($N, 2);
+ }
+ else {
+ .say for min-primes($N, 3);
+ }
+ }
+ }
+}
+
+sub min-primes(UInt $N, $count) {
+ my @primes = (sieve(2, 2..$N) xx 2).flat;
+
+ return @primes.combinations($count).grep(*.sum == $N)
+ .map((*.sort).join(", "))
+ .unique;
+
+ sub sieve($n is copy, @list) {
+ my @primes = @list.grep(* <= $n);
+
+ @primes.append: @list.grep({ $_ > $n and $_ % $n });
+
+ $n = @primes.first(* > $n);
+
+ if $n**2 <= $N {
+ sieve($n, @primes);
+ }
+
+ else {
+ return @primes;
+ }
+ }
+}
diff --git a/challenge-076/mark-anderson/raku/ch-2.raku b/challenge-076/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..5869d511e9
--- /dev/null
+++ b/challenge-076/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,158 @@
+=begin usage
+
+Usage: raku ch-2.raku grid.txt word-list.txt 5
+
+Output:
+
+70 words
+align
+alose
+antes
+arose
+blunt
+blunts
+boids
+broad
+Butea
+Buteas
+clove
+cloven
+constitution
+constitutions
+croon
+depart
+departed
+enter
+filch
+garlic
+gings
+goats
+grieve
+grieves
+grith
+gurrs
+hazard
+ileac
+ileacs
+Ionis
+Iteas
+liens
+lunts
+malign
+malignant
+malls
+midst
+midsts
+neuma
+neumas
+obans
+ought
+ovary
+parted
+pasmo
+quash
+Raias
+raias
+rines
+ruses
+Shrine
+Shrines
+shrine
+shrines
+sices
+social
+soyas
+spasm
+spasmodic
+staun
+succor
+succors
+taars
+talose
+theorem
+theorems
+virus
+viruses
+wifie
+wigged
+
+=end usage
+
+use Lingua::EN::Inflect:from<Perl5> 'PL';
+
+sub MAIN(Str $grid, Str $word-list, UInt $word-length=5) {
+ my @AoA = $grid.IO.lines.map(*.split(/\s/).Array).Array;
+
+ my @strings = strings(@AoA, $word-length);
+
+ my @words = gather {
+ for $word-list.IO.lines -> $word {
+ next if $word.chars < $word-length-1;
+
+ for @strings -> $string {
+ if $string.contains(uc $word) {
+ take $word if $word.chars >= $word-length;
+ my $plural = PL($word);
+ if $plural.chars >= $word-length and $string.contains(uc $plural) {
+ take $plural;
+ }
+ }
+ }
+ }
+ }
+
+ @words = @words.unique;
+ say "{@words.elems} words";
+ .say for @words;
+}
+
+#| returns an array of strings which consists of the horizontal, vertical,
+#| and diagonals of @AoA. It also includes the reverse of each.
+sub strings(@AoA, $word-length) {
+ my @AoA2 = [Z] @AoA;
+ my @AoA3 = @AoA.reverse;
+ my @AoA4 = [Z] @AoA3;
+
+ my @strings;
+ # @AoA horizontal lines
+ @strings.push: $_ for @AoA.map(*.join);
+
+ # @AoA vertical lines
+ @strings.push: $_ for (@AoA2).map(*.join);
+
+ # @AoA upper right diagonals
+ @strings.push: $_ for diagonals(@AoA).map(*.join);
+
+ # @AoA lower left diagonals
+ @strings.push: $_ for diagonals(@AoA2).map(*.join);
+
+ # @AoA lower right diagonals
+ @strings.push: $_ for diagonals(@AoA3).map(*.join);
+
+ # @AoA upper left diagonals
+ @strings.push: $_ for diagonals(@AoA4).map(*.join);
+
+ @strings = @strings.grep(*.chars >= $word-length).unique;
+
+ my @flipped = @strings.map(*.flip);
+
+ @strings.append: @flipped;
+
+ return @strings;
+}
+
+#| there's probably a neat raku trick to get the diagonals.
+sub diagonals(@AoA) {
+ my @diagonals;
+
+ for ^@AoA[0] -> $i {
+ my @diagonal = gather {
+ for ^(@AoA[0]-$i) -> $j {
+ take @AoA[$j][$i+$j] || last;
+ }
+ }
+ @diagonals.push: @diagonal;
+ }
+
+ return @diagonals;
+}