Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #335 (“Common Characters” and “TicTacToe”)
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here:
The Weekly Challenge for the week of 2025-08-18 through 2025-08-24 is #335
The tasks for challenge #335 are as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 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 #2: Input: @words = ("cool", "lock", "cook") Output: ("c", "o") Example #3: Input: @words = ("hello", "world", "pole") Output: ("l", "o") Example #4: Input: @words = ("abc", "def", "ghi") Output: () Example #5: Input: @words = ("aab", "aac", "aaa") Output: ("a", "a")
To solve this problem, I'll make a hash %least to keep track of "least common copies" of each character which is common to all elements of the input array, then for each key $key of %least I'll return $least{$key} copies of '"'.$key.'"'.

Robbie Hatley's Perl Solution to The Weekly Challenge 335-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task 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. 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 ]
There are exactly 8 winning patterns in 3x3 TicTacToe, so finding if someone has won is just a matter of checking the board for each of those 8 patterns, for A and B.
Any one given 3x3 grid is "invalid" if it contains any character other than "A", "B", or "_", or if the number of "winning" patterns is other than 0 or 1.
Any one given 3x3 grid is "won" if exactly one winning pattern is present in either A or B.
Any one given 3x3 grid is "drawn" if every cell is either "A" or "B" and no winning pattern is present.
Any one given 3x3 grid is "pending" if at least one cell is "_" and no winning pattern is present.
So, I'll make a sub that checks for those 5 things and gives either "invalid", "A", "B", "drawn", or "pending" as its output.

Robbie Hatley's Perl Solution to The Weekly Challenge 335-2
That's it for challenge 335; see you on challenge 336!
Comments
Post a Comment