From 084417737a1ea1ddbd8b86a92f5ccb0e872095d4 Mon Sep 17 00:00:00 2001 From: Arne Sommer Date: Sun, 24 Aug 2025 19:57:14 +0200 Subject: week 335 Arne Sommer --- challenge-335/arne-sommer/blog.txt | 1 + challenge-335/arne-sommer/raku/ch-1.raku | 13 +++++++ challenge-335/arne-sommer/raku/ch-2.raku | 45 ++++++++++++++++++++++++ challenge-335/arne-sommer/raku/common-characters | 13 +++++++ challenge-335/arne-sommer/raku/find-winner | 45 ++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 challenge-335/arne-sommer/blog.txt create mode 100755 challenge-335/arne-sommer/raku/ch-1.raku create mode 100755 challenge-335/arne-sommer/raku/ch-2.raku create mode 100755 challenge-335/arne-sommer/raku/common-characters create mode 100755 challenge-335/arne-sommer/raku/find-winner diff --git a/challenge-335/arne-sommer/blog.txt b/challenge-335/arne-sommer/blog.txt new file mode 100644 index 0000000000..b3f45a2085 --- /dev/null +++ b/challenge-335/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/common-find.html \ No newline at end of file diff --git a/challenge-335/arne-sommer/raku/ch-1.raku b/challenge-335/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..423b3fa202 --- /dev/null +++ b/challenge-335/arne-sommer/raku/ch-1.raku @@ -0,0 +1,13 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@words where @words.elems > 0, :v(:$verbose)); + +my @bags = @words>>.comb>>.Bag; + +say ": Bags; { @bags.raku }" if $verbose; + +my $intersection = [(&)] @bags; + +say ": Common: { $intersection.raku }" if $verbose; + +say "(" ~ $intersection.kxxv.sort.map( '"' ~ * ~ '"' ).join(", ") ~ ")"; \ No newline at end of file diff --git a/challenge-335/arne-sommer/raku/ch-2.raku b/challenge-335/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..7998fc654d --- /dev/null +++ b/challenge-335/arne-sommer/raku/ch-2.raku @@ -0,0 +1,45 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@moves where @moves.elems > 0 + && all(@moves) ~~/^<[012]> \, <[012]>$/, + :v(:$verbose)); + +my @board = [ [ '-', '-', '-' ], ['-', '-', '-'], ['-', '-', '-'] ]; + +my $player = 'A'; + +for @moves -> $move +{ + my ($x, $y) = $move.split(","); + + die "Position $x,$y already taken" unless @board[$x][$y] eq "-"; + + @board[$x][$y] = $player; + + $player = $player eq 'A' ?? 'B' !! 'A'; +} + +if $verbose +{ + say ": " ~ @board[0].join(" "); + say ": " ~ @board[1].join(" "); + say ": " ~ @board[2].join(" "); +} + +for 'A', 'B' -> $ab +{ + if all(@board[0;*]) eq $ab || + all(@board[1;*]) eq $ab || + all(@board[2;*]) eq $ab || + all(@board[*;0]) eq $ab || + all(@board[*;1]) eq $ab || + all(@board[*;2]) eq $ab || + @board[0][0] eq @board[1][1] eq @board[2][2] eq $ab || + @board[2][0] eq @board[1][1] eq @board[0][2] eq $ab + { + say $ab; + exit; + } +} + +say @moves.elems == 9 ?? 'Draw' !! 'Pending'; diff --git a/challenge-335/arne-sommer/raku/common-characters b/challenge-335/arne-sommer/raku/common-characters new file mode 100755 index 0000000000..423b3fa202 --- /dev/null +++ b/challenge-335/arne-sommer/raku/common-characters @@ -0,0 +1,13 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@words where @words.elems > 0, :v(:$verbose)); + +my @bags = @words>>.comb>>.Bag; + +say ": Bags; { @bags.raku }" if $verbose; + +my $intersection = [(&)] @bags; + +say ": Common: { $intersection.raku }" if $verbose; + +say "(" ~ $intersection.kxxv.sort.map( '"' ~ * ~ '"' ).join(", ") ~ ")"; \ No newline at end of file diff --git a/challenge-335/arne-sommer/raku/find-winner b/challenge-335/arne-sommer/raku/find-winner new file mode 100755 index 0000000000..7998fc654d --- /dev/null +++ b/challenge-335/arne-sommer/raku/find-winner @@ -0,0 +1,45 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@moves where @moves.elems > 0 + && all(@moves) ~~/^<[012]> \, <[012]>$/, + :v(:$verbose)); + +my @board = [ [ '-', '-', '-' ], ['-', '-', '-'], ['-', '-', '-'] ]; + +my $player = 'A'; + +for @moves -> $move +{ + my ($x, $y) = $move.split(","); + + die "Position $x,$y already taken" unless @board[$x][$y] eq "-"; + + @board[$x][$y] = $player; + + $player = $player eq 'A' ?? 'B' !! 'A'; +} + +if $verbose +{ + say ": " ~ @board[0].join(" "); + say ": " ~ @board[1].join(" "); + say ": " ~ @board[2].join(" "); +} + +for 'A', 'B' -> $ab +{ + if all(@board[0;*]) eq $ab || + all(@board[1;*]) eq $ab || + all(@board[2;*]) eq $ab || + all(@board[*;0]) eq $ab || + all(@board[*;1]) eq $ab || + all(@board[*;2]) eq $ab || + @board[0][0] eq @board[1][1] eq @board[2][2] eq $ab || + @board[2][0] eq @board[1][1] eq @board[0][2] eq $ab + { + say $ab; + exit; + } +} + +say @moves.elems == 9 ?? 'Draw' !! 'Pending'; -- cgit