diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-08-18 09:45:54 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-08-18 09:45:54 +0200 |
| commit | af3573ff08bdf09be30a0815feba5312ab72dec9 (patch) | |
| tree | 1ed0aedb365c3cfa90494ff900dde3ef619e1073 | |
| parent | 4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff) | |
| download | perlweeklychallenge-club-af3573ff08bdf09be30a0815feba5312ab72dec9.tar.gz perlweeklychallenge-club-af3573ff08bdf09be30a0815feba5312ab72dec9.tar.bz2 perlweeklychallenge-club-af3573ff08bdf09be30a0815feba5312ab72dec9.zip | |
Add solutions to 335: Common Characters & Find Winner by E. Choroba
| -rwxr-xr-x | challenge-335/e-choroba/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-335/e-choroba/perl/ch-2.pl | 40 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-335/e-choroba/perl/ch-1.pl b/challenge-335/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c6d02368a5 --- /dev/null +++ b/challenge-335/e-choroba/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ min }; + +sub common_characters(@words) { + my %seen; + for my $i (0 .. $#words) { + ++$seen{$_}[$i] for split //, $words[$i]; + } + return [map +($_) x min(map $_ // 0, @{ $seen{$_} }[0 .. $#words]), + keys %seen] +} + +use Test2::V0; +plan(5); + +is common_characters('bella', 'label', 'roller'), + bag { item $_ for qw( e l l ); end() }, 'Example 1'; +is common_characters('cool', 'lock', 'cook'), + bag { item $_ for qw( c o ); end() }, 'Example 2'; +is common_characters('hello', 'world', 'pole'), + bag { item $_ for qw( l o ); end() }, 'Example 3'; +is common_characters('abc', 'def', 'ghi'), [], 'Example 4'; +is common_characters('aab', 'aac', 'aaa'), + bag { item $_ for qw( a a ); end() }, 'Example 5'; diff --git a/challenge-335/e-choroba/perl/ch-2.pl b/challenge-335/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..6d2397c4e0 --- /dev/null +++ b/challenge-335/e-choroba/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub find_winner(@moves) { + my @grid; + for my $i (0 .. $#moves) { + $grid[ $moves[$i][0] ][ $moves[$i][1] ] = qw( A B )[ $i % 2 ]; + } + my @patterns; + push @patterns, [[0, 0], [1, 1], [2, 2]], + [[0, 2], [1, 1], [2, 0]]; + push @patterns, map { my $x = $_; [map [$x, $_], 0 .. 2] } 0 .. 2; + push @patterns, map { my $y = $_; [map [$_, $y], 0 .. 2] } 0 .. 2; + + for my $pattern (@patterns) { + my %seen; + ++$seen{ $grid[ $_->[0] ][ $_->[1] ] // "" } for @$pattern; + ($seen{$_} // 0) == 3 and return $_ for qw( A B ); + } + + return @moves < 9 ? 'Pending' : 'Draw' +} + +use Test::More tests => 5 + 3; + +is find_winner([0, 0], [2, 0], [1, 1], [2, 1], [2, 2]), 'A', 'Example 1'; +is find_winner([0, 0], [1, 1], [0, 1], [0, 2], [1, 0], [2, 0]), 'B', + 'Example 2'; +is find_winner([0, 0], [1, 1], [2, 0], [1, 0], [1, 2], [2, 1], [0, 1], + [0, 2], [2, 2]), + 'Draw', 'Example 3'; +is find_winner([0, 0], [1, 1]), 'Pending', 'Example 4'; +is find_winner([1, 1], [0, 0], [2, 2], [0, 1], [1, 0], [0, 2]), 'B', + 'Example 5'; + +is find_winner([2, 0], [1, 0], [2, 1], [1, 1], [2, 2]), 'A', 'A row 2'; +is find_winner([1, 1], [0, 0], [1, 0], [2, 2], [1, 2]), 'A', 'A col 1'; +is find_winner(), 'Pending', 'Empty'; |
