aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-23 23:28:14 +0100
committerGitHub <noreply@github.com>2025-08-23 23:28:14 +0100
commita923e9810522db4d2ade458a4e9b670976f049ae (patch)
tree4949785a90902ed8dccaf38ca549d95ab079d07d
parent2f9aa438571720a94561023e20f1cb053c976a72 (diff)
parent4b63b52698e1ad843dc8c25188626b1cbfaaf451 (diff)
downloadperlweeklychallenge-club-a923e9810522db4d2ade458a4e9b670976f049ae.tar.gz
perlweeklychallenge-club-a923e9810522db4d2ade458a4e9b670976f049ae.tar.bz2
perlweeklychallenge-club-a923e9810522db4d2ade458a4e9b670976f049ae.zip
Merge pull request #12556 from 0rir/work
335
-rw-r--r--challenge-335/0rir/raku/ch-1.raku54
-rw-r--r--challenge-335/0rir/raku/ch-2.raku129
2 files changed, 183 insertions, 0 deletions
diff --git a/challenge-335/0rir/raku/ch-1.raku b/challenge-335/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..4b0ab37b9d
--- /dev/null
+++ b/challenge-335/0rir/raku/ch-1.raku
@@ -0,0 +1,54 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰βŠ† ≑ β‰’ «␀ Β» ∴ 🐧
+use v6.d;
+use Test;
+
+=begin comment
+335-1: Common Characters Submitted by: Mohammad Sajid Anwar
+You are given an array of words.
+
+Write a script to return all characters that is in every word in the given array including duplicates.
+
+Example 1
+Input: @words = ("bella", "label", "roller")
+Output: ("e", "l", "l")
+…
+Example 4
+Input: @words = ("abc", "def", "ghi")
+Output: ()
+=end comment
+
+my @Test = # in #exp
+ ("bella", "label", "roller"), ("e", "l", "l"),
+ ("cool", "lock", "cook"), ("c", "o"),
+ ("hello", "world", "pole"), ("l", "o"),
+ ("abc", "def", "ghi"), (),
+ ("aab", "aac", "aaa"), ("a", "a"),
+;
+plan +@Test Γ· 2;
+
+# Intersect of BagHashes is like Set ∩ but using the lesser quantity.
+sub infix:<intersect>( BagHash $a, BagHash $b -->BagHash) {
+ my BagHash $ret;
+ my @key = ( $a.keys.Set ∩ $a.keys.Set).keys;
+ for @key -> \k {
+ $ret{k} = $a{k} min $b{k};
+ }
+ $ret
+}
+
+# Expand a BagHash to a List by adding each key by its value times;
+multi expand( BagHash:U $a -->List ) { () }
+multi expand( BagHash:D $a -->List) { $a.kxxv.sort.List }
+
+sub task( @in -->List) { expand [intersect] (@inΒ».comb)Β».BagHash; }
+
+for @Test -> $in, $exp {
+ is task( $in), $exp, "{$exp // $exp.^name()} <- $in.raku()";
+}
+done-testing;
+
+my @word = "cool", "lock", "cook";
+
+say "\nInput: @word = @word.raku()\nOutput: (\"",
+ task( @word).sort.join('", "'), '")';
diff --git a/challenge-335/0rir/raku/ch-2.raku b/challenge-335/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..86066bfa38
--- /dev/null
+++ b/challenge-335/0rir/raku/ch-2.raku
@@ -0,0 +1,129 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # πŸ¦‹ βˆ…βˆͺβˆ©βˆ‹βˆˆβˆ‰βŠ† ≑ β‰’ «␀ Β» ∴ 🐧
+use v6.d;
+use Test;
+
+=begin comment
+335-2: Find Winner Submitted by: Mohammad Sajid Anwar
+You are given an array of all moves by the two players.
+
+Write a script to find the winner of the TicTacToe game if found based on the moves provided in the given array.
+
+UPDATE: Order move is in the order - A, B, A, B, A, ….
+
+Example 1
+Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2])
+Output: A
+
+Game Board:
+[ A _ _ ]
+[ B A B ]
+[ _ _ A ]
+
+Example 2
+Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0])
+Output: B
+
+Game Board:
+[ A A B ]
+[ A B _ ]
+[ B _ _ ]
+
+Example 3
+Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2])
+Output: Draw
+
+Game Board:
+[ A A B ]
+[ B B A ]
+[ A B A ]
+
+Example 4
+Input: @moves = ([0,0],[1,1])
+Output: Pending
+
+Game Board:
+[ A _ _ ]
+[ _ B _ ]
+[ _ _ _ ]
+
+Example 5
+Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2])
+Output: B
+
+Game Board:
+[ B B B ]
+[ A A _ ]
+[ _ _ A ]
+=end comment
+
+constant \EARLY = 4;
+constant \DONE = 9;
+constant \PENDING = 'Pending';
+constant \DRAW = 'Draw';
+
+
+my @Test =
+ ([0,0],[2,0],[1,1],[2,1],[2,2]), 'A', # -> SE
+ ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]), 'B', # -> SW
+ ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]), DRAW,
+ ([0,0],[1,1]), PENDING, # short
+ ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]), 'B', # -> E
+ ([0,0],[1,1],[0,1],[1,0],[0,2]), 'A', # -> E
+ ([0,0],[2,1],[1,0],[1,1],[2,0]), 'A', # -> S
+ ([1,1],[1,2],[2,2],[0,0],[2,1],[2,0],[0,1]), 'A',
+;
+plan +@Test Γ· 2;
+
+constant \_ = '_'; # empty tic
+my @player = <A B>;
+my @board[3;3];
+
+# Could not get binding twixt array elems to work.
+
+# Return True if $player has 3 in a row.
+sub win-q( @b, $player, $r, $c -->Bool) {
+ return True if (@b[$r;0],@b[$r;1],@b[$r;2]).all eq $player; # row
+
+ return True if (@b[0;$c],@b[1;$c],@b[2;$c]).all eq $player; # column
+
+ return True if (@b[0;0], @b[1;1], @b[2;2]).all eq $player # \ slant
+ and ($r,$c) ~~ ((0,0), (1,1), (2,2)).any;
+
+ return True if (@b[0;2], @b[1;1], @b[2;0]).all eq $player # / slant
+ and ($r, $c) ~~ ((0,2), (1,1), (2,0)).any;
+ False;
+}
+
+sub task( @move -->Str) {
+ my $player = True;
+ my @board[3;3] = [ [_,_,_],[_,_,_],[_,_,_]]; #empty the array;
+
+ return PENDING if @move.elems ≀ EARLY;
+
+ my $i = 0;
+ while $i <= min DONE, @move.end {
+ my $p = @player[$player.=not]; # toggle player
+ my $r = @move[$i;0];
+ my $c = @move[$i;1];
+ @board[ $r;$c] = $p;
+ if win-q( @board, $p, $r, $c) {
+ return $p;
+ }
+ ++$i;
+ }
+ given $i {
+ when DONE { DRAW }
+ default { PENDING}
+ }
+}
+
+for @Test -> @in, $exp {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+}
+done-testing;
+
+my @move = [0,0],[2,0],[1,1],[2,1],[2,2];
+
+say "\nInput: @move = @move.raku()\nOutput: {task @move}";
+