aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-11-24 15:58:30 +0100
committerE. Choroba <choroba@matfyz.cz>2025-11-24 15:58:30 +0100
commit4dc279bc734c3ea6c8fe3ab23ff64d4f3f82d293 (patch)
treeef2ffa7f4c42b8e85c87b1b2928887b664ee2286
parent2d11842284e2c5fdab012d81a282d6b466f72572 (diff)
downloadperlweeklychallenge-club-4dc279bc734c3ea6c8fe3ab23ff64d4f3f82d293.tar.gz
perlweeklychallenge-club-4dc279bc734c3ea6c8fe3ab23ff64d4f3f82d293.tar.bz2
perlweeklychallenge-club-4dc279bc734c3ea6c8fe3ab23ff64d4f3f82d293.zip
Add solutions to 349: Power String & Meeting Point by E. Choroba
-rwxr-xr-xchallenge-349/e-choroba/perl/ch-1.pl22
-rwxr-xr-xchallenge-349/e-choroba/perl/ch-2.pl59
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-349/e-choroba/perl/ch-1.pl b/challenge-349/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..a6c42957db
--- /dev/null
+++ b/challenge-349/e-choroba/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ max };
+
+sub power_string($str) {
+ my $l = 0;
+ $l < length $1 and $l = length $1 while $str =~ /((.)\2*)/g;
+ return $l
+}
+
+use Test::More tests => 5 + 1;
+
+is power_string('textbook'), 2, 'Example 1';
+is power_string('aaaaa'), 5, 'Example 2';
+is power_string('hoorayyy'), 3, 'Example 3';
+is power_string('x'), 1, 'Example 4';
+is power_string('aabcccddeeffffghijjk'), 4, 'Example 5';
+
+is power_string(""), 0, 'Zero';
diff --git a/challenge-349/e-choroba/perl/ch-2.pl b/challenge-349/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..e09339ead4
--- /dev/null
+++ b/challenge-349/e-choroba/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+{ my %STEP = (U => [0, -1],
+ D => [0, 1],
+ L => [-1, 0],
+ R => [1, 0]);
+ sub meeting_point_coord($path) {
+ my ($x, $y) = (0, 0);
+ for my $step (split //, $path) {
+ $x += $STEP{$step}->[0];
+ $y += $STEP{$step}->[1];
+ return 1 if 0 == $x && 0 == $y;
+ }
+ return
+ }
+}
+
+sub meeting_point($path) {
+ my %distance = map +($_ => 0), qw( U D L R );
+ for my $step (split //, $path) {
+ ++$distance{$step};
+ return 1 if $distance{U} == $distance{D}
+ && $distance{L} == $distance{R};
+ }
+ return
+}
+
+use Test2::V0 -no_srand;
+plan(2 * 5 + 2);
+
+use constant {
+ true => bool(1),
+ false => bool(0)};
+
+for my $test ([\&meeting_point, 'hash'], [\&meeting_point_coord, 'coord']) {
+ is $test->[0]('ULD'), false, 'Example 1 ' . $test->[1];
+ is $test->[0]('ULDR'), true, 'Example 2 ' . $test->[1];
+ is $test->[0]('UUURRRDDD'), false, 'Example 3 ' . $test->[1];
+ is $test->[0]('UURRRDDLLL'), true, 'Example 4 ' . $test->[1];
+ is $test->[0]('RRUULLDDRRUU'), true, 'Example 5 ' . $test->[1];
+}
+
+# Interestingly, using coordinates is faster for shorter paths, while
+# the hash approach is faster for longer strings.
+use Benchmark qw{ cmpthese };
+for my $path('UULLDDRR', 'UULLDR' x 250) {
+ my $mp = meeting_point($path);
+
+ is $mp, meeting_point_coord($path),
+ 'Same length(' . length($path) . ') ' . ($mp ? 1 : 0);
+
+ cmpthese(-3, {
+ hash => sub { meeting_point($path) },
+ coord => sub { meeting_point_coord($path) }
+ });
+}