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 From a2293cf4868fa5df03d214d4744114ed7cecdd64 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 18 Aug 2025 08:40:15 +0000 Subject: Challenge 335 Solutions (Raku) --- challenge-335/mark-anderson/raku/ch-2.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-335/mark-anderson/raku/ch-2.raku b/challenge-335/mark-anderson/raku/ch-2.raku index d81359fac2..f3a580d72c 100644 --- a/challenge-335/mark-anderson/raku/ch-2.raku +++ b/challenge-335/mark-anderson/raku/ch-2.raku @@ -25,18 +25,18 @@ sub find-winner(+@moves) @moves[$seq].map({ @board[.[0];.[1]] = $ltr }); # check rows - return $ltr if @board.first({ [eq] .flat }); + return $ltr if @board.first({ .all eq $ltr }); # check upper left to lower right diagonal my @a = (^Inf) Z (^Inf).head(@board); - return $ltr if [eq] @a.map({ @board[.[0];.[1]] }); + return $ltr if @a.map({ @board[.[0];.[1]] }).all eq $ltr; # check upper right to lower left diagonal @a = (^Inf) Z (@board.end...0); - return $ltr if [eq] @a.map({ @board[.[0];.[1]] }); + return $ltr if @a.map({ @board[.[0];.[1]] }).all eq $ltr; # check columns - return $ltr if ([Z] @board).first({ [eq] .flat }); + return $ltr if ([Z] @board).first({ .all eq $ltr }); return 'Draw' } -- cgit From b5c0dbb2e29465fb900da10b404beab74b012348 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 18 Aug 2025 09:11:28 +0000 Subject: Challenge 335 Solutions (Raku) --- challenge-335/mark-anderson/raku/ch-2.raku | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/challenge-335/mark-anderson/raku/ch-2.raku b/challenge-335/mark-anderson/raku/ch-2.raku index f3a580d72c..fdc525fa0f 100644 --- a/challenge-335/mark-anderson/raku/ch-2.raku +++ b/challenge-335/mark-anderson/raku/ch-2.raku @@ -24,19 +24,22 @@ sub find-winner(+@moves) @moves[$seq].map({ @board[.[0];.[1]] = $ltr }); - # check rows - return $ltr if @board.first({ .all eq $ltr }); - - # check upper left to lower right diagonal - my @a = (^Inf) Z (^Inf).head(@board); - return $ltr if @a.map({ @board[.[0];.[1]] }).all eq $ltr; - - # check upper right to lower left diagonal - @a = (^Inf) Z (@board.end...0); - return $ltr if @a.map({ @board[.[0];.[1]] }).all eq $ltr; - - # check columns - return $ltr if ([Z] @board).first({ .all eq $ltr }); + return $ltr if any + ( + # check rows + @board.first({ .all eq $ltr }), + + # check upper left to lower right diagonal + ((^Inf) Z (^Inf).head(@board)) + .map({ @board[.[0];.[1]] }).all eq $ltr, + + # check upper right to lower left diagonal + ((^Inf) Z (@board.end...0)) + .map({ @board[.[0];.[1]] }).all eq $ltr, + + # check columns + ([Z] @board).first({ .all eq $ltr }) + ); return 'Draw' } -- cgit From 780da73208c2e7e6d23ccb5fc4ffabb75024085c Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 18 Aug 2025 09:27:21 +0000 Subject: Challenge 335 Solutions (Raku) --- challenge-335/mark-anderson/raku/ch-2.raku | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-335/mark-anderson/raku/ch-2.raku b/challenge-335/mark-anderson/raku/ch-2.raku index fdc525fa0f..e879873c1c 100644 --- a/challenge-335/mark-anderson/raku/ch-2.raku +++ b/challenge-335/mark-anderson/raku/ch-2.raku @@ -1,7 +1,7 @@ #!/usr/bin/env raku use Test; -# This solution allows for higher dimensional tic tac toe boards +# 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 @@ -17,7 +17,7 @@ sub find-winner(+@moves) { my $order = @moves[*;*].max + 1; - my @board = ('_' xx $order).Array xx $order; + my @board = (0 xx $order).Array xx $order; my $ltr = @moves.end %% 2 ?? 'A' !! 'B'; my $seq := @moves.end %% 2 ?? (0,2...*) !! (1,3...*); @@ -27,18 +27,18 @@ sub find-winner(+@moves) return $ltr if any ( # check rows - @board.first({ .all eq $ltr }), + @board.first.all, # check upper left to lower right diagonal ((^Inf) Z (^Inf).head(@board)) - .map({ @board[.[0];.[1]] }).all eq $ltr, + .map({ @board[.[0];.[1]] }).all, # check upper right to lower left diagonal ((^Inf) Z (@board.end...0)) - .map({ @board[.[0];.[1]] }).all eq $ltr, + .map({ @board[.[0];.[1]] }).all, # check columns - ([Z] @board).first({ .all eq $ltr }) + ([Z] @board).first.all ); return 'Draw' -- cgit From cfefb52e2e3dfc8d7b58f7d98bfb1bb6e5e82884 Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 18 Aug 2025 10:02:54 +0000 Subject: Challenge 335 Solutions (Raku) --- challenge-335/mark-anderson/raku/ch-2.raku | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/challenge-335/mark-anderson/raku/ch-2.raku b/challenge-335/mark-anderson/raku/ch-2.raku index e879873c1c..e8f8c342ed 100644 --- a/challenge-335/mark-anderson/raku/ch-2.raku +++ b/challenge-335/mark-anderson/raku/ch-2.raku @@ -16,7 +16,6 @@ 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'; @@ -30,12 +29,10 @@ sub find-winner(+@moves) @board.first.all, # check upper left to lower right diagonal - ((^Inf) Z (^Inf).head(@board)) - .map({ @board[.[0];.[1]] }).all, + (^@board).map({@board[$_;$_]}).all, # check upper right to lower left diagonal - ((^Inf) Z (@board.end...0)) - .map({ @board[.[0];.[1]] }).all, + (^@board).map({@board[$_;@board.end-$_]}).all, # check columns ([Z] @board).first.all -- cgit