From 2fcfde68609de0e095f7fdec8eb11f660d766517 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 18 Aug 2025 07:56:14 +0000 Subject: Challenge 335 Solutions (Raku) --- challenge-335/mark-anderson/raku/ch-1.raku | 14 ++++++++++ challenge-335/mark-anderson/raku/ch-2.raku | 42 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 challenge-335/mark-anderson/raku/ch-1.raku create mode 100644 challenge-335/mark-anderson/raku/ch-2.raku 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..d81359fac2 --- /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 = ('_' 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 }); + + # check rows + return $ltr if @board.first({ [eq] .flat }); + + # check upper left to lower right diagonal + my @a = (^Inf) Z (^Inf).head(@board); + return $ltr if [eq] @a.map({ @board[.[0];.[1]] }); + + # check upper right to lower left diagonal + @a = (^Inf) Z (@board.end...0); + return $ltr if [eq] @a.map({ @board[.[0];.[1]] }); + + # check columns + return $ltr if ([Z] @board).first({ [eq] .flat }); + + return 'Draw' +} -- cgit