diff options
| author | Mark Anderson <mark@andemark.io> | 2025-08-18 07:56:14 +0000 |
|---|---|---|
| committer | Mark Anderson <mark@andemark.io> | 2025-08-18 07:56:14 +0000 |
| commit | 2fcfde68609de0e095f7fdec8eb11f660d766517 (patch) | |
| tree | 16debc9307d5c91fbd0a8c14a0f26f361864c6ea | |
| parent | 4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff) | |
| download | perlweeklychallenge-club-2fcfde68609de0e095f7fdec8eb11f660d766517.tar.gz perlweeklychallenge-club-2fcfde68609de0e095f7fdec8eb11f660d766517.tar.bz2 perlweeklychallenge-club-2fcfde68609de0e095f7fdec8eb11f660d766517.zip | |
Challenge 335 Solutions (Raku)
| -rw-r--r-- | challenge-335/mark-anderson/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-335/mark-anderson/raku/ch-2.raku | 42 |
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..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' +} |
