aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-19 11:46:58 +0100
committerGitHub <noreply@github.com>2025-08-19 11:46:58 +0100
commit69be3a3fcc13b3ca8ef4bf1ecd7440ad69cf9426 (patch)
tree2b7ee4cdff3a2a744a22e6564e65f3f51d2235a9
parent49337a2ca8f973c1fb32289580f7d0d51fcb3bf6 (diff)
parentcfefb52e2e3dfc8d7b58f7d98bfb1bb6e5e82884 (diff)
downloadperlweeklychallenge-club-69be3a3fcc13b3ca8ef4bf1ecd7440ad69cf9426.tar.gz
perlweeklychallenge-club-69be3a3fcc13b3ca8ef4bf1ecd7440ad69cf9426.tar.bz2
perlweeklychallenge-club-69be3a3fcc13b3ca8ef4bf1ecd7440ad69cf9426.zip
Merge pull request #12534 from andemark/challenge-335
Challenge 335 Solutions (Raku)
-rw-r--r--challenge-335/mark-anderson/raku/ch-1.raku14
-rw-r--r--challenge-335/mark-anderson/raku/ch-2.raku42
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-335/mark-anderson/raku/ch-1.raku b/challenge-335/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..fa5e9bf1fb
--- /dev/null
+++ b/challenge-335/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/env raku
+use Test;
+
+is-deeply common-characters(< bella label roller >), < e l l >;
+is-deeply common-characters(< cool lock cook >), < c o >;
+is-deeply common-characters(< hello world pole >), < l o >;
+is-deeply common-characters(< abc def ghi >), ();
+is-deeply common-characters(< aab aac aaa >), < a a >;
+
+sub common-characters(@words)
+{
+ my @bags = @words>>.comb>>.Bag;
+ ([(&)] @bags).kxxv.sort
+}
diff --git a/challenge-335/mark-anderson/raku/ch-2.raku b/challenge-335/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..e8f8c342ed
--- /dev/null
+++ b/challenge-335/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,42 @@
+#!/usr/bin/env raku
+use Test;
+
+# This solution allows for higher dimensional tic-tac-toe boards
+# but it doesn't take into account any special rules those may have.
+
+# I didn't check for 'pending' because that's hard to check unless
+# you're also given the board size.
+
+is find-winner([0,0],[2,0],[1,1],[2,1],[2,2]), 'A';
+is find-winner([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]), 'B';
+is find-winner([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]), 'Draw';
+flunk 'Pending not implemented'; find-winner([0,0],[1,1]);
+is find-winner([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]), 'B';
+
+sub find-winner(+@moves)
+{
+ my $order = @moves[*;*].max + 1;
+ my @board = (0 xx $order).Array xx $order;
+
+ my $ltr = @moves.end %% 2 ?? 'A' !! 'B';
+ my $seq := @moves.end %% 2 ?? (0,2...*) !! (1,3...*);
+
+ @moves[$seq].map({ @board[.[0];.[1]] = $ltr });
+
+ return $ltr if any
+ (
+ # check rows
+ @board.first.all,
+
+ # check upper left to lower right diagonal
+ (^@board).map({@board[$_;$_]}).all,
+
+ # check upper right to lower left diagonal
+ (^@board).map({@board[$_;@board.end-$_]}).all,
+
+ # check columns
+ ([Z] @board).first.all
+ );
+
+ return 'Draw'
+}