diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-23 16:24:15 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-23 16:24:15 +0100 |
| commit | adca5b9c327e96e197dec88fc7872d988f84eacf (patch) | |
| tree | e608735ff7d96c8ad820a559909f1b8f64f6404f | |
| parent | 89c11ad8c143a56322780e2bbdfeddd09951a749 (diff) | |
| download | perlweeklychallenge-club-adca5b9c327e96e197dec88fc7872d988f84eacf.tar.gz perlweeklychallenge-club-adca5b9c327e96e197dec88fc7872d988f84eacf.tar.bz2 perlweeklychallenge-club-adca5b9c327e96e197dec88fc7872d988f84eacf.zip | |
Week 335 - Common winner
| -rw-r--r-- | challenge-335/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-335/peter-campbell-smith/perl/ch-1.pl | 46 | ||||
| -rwxr-xr-x | challenge-335/peter-campbell-smith/perl/ch-2.pl | 60 |
3 files changed, 107 insertions, 0 deletions
diff --git a/challenge-335/peter-campbell-smith/blog.txt b/challenge-335/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..6735e287c3 --- /dev/null +++ b/challenge-335/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/335 diff --git a/challenge-335/peter-campbell-smith/perl/ch-1.pl b/challenge-335/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..35059971ab --- /dev/null +++ b/challenge-335/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-18 +use utf8; # Week 335 - task 1 - Common characters +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +common_characters('bella', 'label', 'roller'); +common_characters('cool', 'lock', 'cook'); +common_characters('hello', 'world', 'pole'); +common_characters('abc', 'def', 'ghi'); +common_characters('aab', 'aac', 'aaa'); + +sub common_characters { + + my (@words, @chars, $count, $c, $min, $output, $w, $k); + + no warnings 'uninitialized'; + @words = @_; + + # create count of character $c in word $w + for $w (0 .. $#words) { + @chars = split('', $words[$w]); + $count->{$chars[$_]}->[$w] ++ for 0 .. $#chars; + } + + # loop $c over characters + for $c (sort keys %$count) { + + # find minimum number of occurrences of $c in all words + $min = 1e6; + for $w (0 .. $#words) { + $k = $count->{$c}->[$w]; + $min = $k if $k < $min; + } + + # output that number of occurrences of $c + $output .= qq['$c', ] for 1 .. $min; + } + + # report + say qq[\nInput: ('] . join(q[', '], @words) . q[')]; + say qq[Output: (] . substr($output, 0, -2) . ')'; +} diff --git a/challenge-335/peter-campbell-smith/perl/ch-2.pl b/challenge-335/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..8ec480c99d --- /dev/null +++ b/challenge-335/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-18 +use utf8; # Week 335 - task 2 - Find winner +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +find_winner([0,0],[2,0],[1,1],[2,1],[2,2]); +find_winner([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]); +find_winner([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]); +find_winner([0,0],[1,1]); +find_winner([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]); + +sub find_winner { + + my ($input, @wins, @moves, $player, @owned, $move, $win, $output, $j, @board); + + # initialise + @moves = @_; + $player = 0; + @owned = (0, 0); + $board[$_] = '_' for 0 .. 8; + + # the 8 winning patterns + @wins = (0b111000000, 0b000111000, 0b000000111, + 0b100100100, 0b010010010, 0b001001001, + 0b100010001, 0b001010100); + + # loop over moves and set player as owning the cell + for $move (@moves) { + $owned[$player] |= (2 ** (3 * $move->[0] + $move->[1])); + $board[$move->[1] + 3 * $move->[0]] = chr(ord('A') + $player); + $player = 1 - $player; + } + + # check to see if this move makes a win + for $win (@wins) { + for $j (0, 1) { + if (($win & $owned[$j]) == $win) { + $output = chr(ord('A') + $j) . ' wins'; + last; + } + } + } + + # draw if no win with 9 moves or pending id <9 moves + $output = scalar @moves == 9 ? 'Draw' : 'Pending' unless $output; + + # report + $input .= qq{[$_->[0], $_->[1]], } for @moves; + say qq[\nInput: ] . substr($input, 0, -2); + say qq[Output: $output]; + + # show board + say qq{ [ $board[0] $board[1] $board[2] ]\n} . + qq{ [ $board[3] $board[4] $board[5] ]\n} . + qq{ [ $board[6] $board[7] $board[8] ]}; +}
\ No newline at end of file |
