aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Shitov <ash@iMacM3.local>2024-02-12 20:11:46 +0100
committerAndrey Shitov <ash@iMacM3.local>2024-02-12 20:11:46 +0100
commit1a7c1c982a6c1105ced55cae4cf2d58e49e9c67b (patch)
tree2d325bfe3ffe0154618f4fa7b16eb5f7c6ae7fd7
parent3f3e0798a68401ce1d67a5e1534f69de16856e82 (diff)
downloadperlweeklychallenge-club-1a7c1c982a6c1105ced55cae4cf2d58e49e9c67b.tar.gz
perlweeklychallenge-club-1a7c1c982a6c1105ced55cae4cf2d58e49e9c67b.tar.bz2
perlweeklychallenge-club-1a7c1c982a6c1105ced55cae4cf2d58e49e9c67b.zip
Week 256, solutions in Raku by ash
-rw-r--r--challenge-256/ash/raku/ch-1.raku25
-rw-r--r--challenge-256/ash/raku/ch-2.raku27
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-256/ash/raku/ch-1.raku b/challenge-256/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..afab0079c5
--- /dev/null
+++ b/challenge-256/ash/raku/ch-1.raku
@@ -0,0 +1,25 @@
+# Solution to the Task 1 of the Weekly Challenge #256
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK1
+
+# Test run:
+# % raku ch-1.raku
+# 1
+# 0
+# 2
+
+my @tests =
+ ('ab', 'de', 'ed', 'bc'),
+ ('aa', 'ba', 'cd', 'ed'),
+ ('uv', 'qp', 'st', 'vu', 'mn', 'pq');
+
+for @tests -> @words {
+ say count-maximum-pairs(@words);
+}
+
+sub count-maximum-pairs(@words) {
+ my @rev = @words.map: *.flip;
+ my $words = (@words, @rev).Bag;
+
+ my $pairs = $words.grep: *.value == 2; # Each pair is counted twice
+ return $pairs.elems div 2;
+}
diff --git a/challenge-256/ash/raku/ch-2.raku b/challenge-256/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..e6915815e4
--- /dev/null
+++ b/challenge-256/ash/raku/ch-2.raku
@@ -0,0 +1,27 @@
+# Solution to the Task 2 of the Weekly Challenge #256
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/#TASK2
+
+# Test run:
+# $ raku ch-2.raku
+# a1b2c3d4
+# a1b2c345
+# a1b2c3de
+
+my @tests =
+ ('abcd', '1234'),
+ ('abc', '12345'),
+ ('abcde', '123');
+
+for @tests -> ($str1, $str2) {
+ say merge-strings($str1, $str2);
+}
+
+sub merge-strings($str1, $str2) {
+ my $merged = ($str1.comb Z~ $str2.comb).join;
+
+ given $str1.chars - $str2.chars {
+ return $merged ~ $str1.substr($str2.chars) when * > 0;
+ return $merged ~ $str2.substr($str1.chars) when * < 0;
+ return $merged;
+ }
+}