diff options
| author | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-11-24 12:01:38 +0100 |
|---|---|---|
| committer | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-11-24 12:01:38 +0100 |
| commit | cb80fee1fbc25e093a382be4376daa1f4b810f5e (patch) | |
| tree | 8a7985b06f8f7d115e99797ed60d4b9a02eab111 | |
| parent | f26c24e25034b8b1c61ee2f734d52221b98ebbd4 (diff) | |
| download | perlweeklychallenge-club-cb80fee1fbc25e093a382be4376daa1f4b810f5e.tar.gz perlweeklychallenge-club-cb80fee1fbc25e093a382be4376daa1f4b810f5e.tar.bz2 perlweeklychallenge-club-cb80fee1fbc25e093a382be4376daa1f4b810f5e.zip | |
Challenge 349
| -rw-r--r-- | challenge-349/mahnkong/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-349/mahnkong/perl/ch-2.pl | 36 |
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-349/mahnkong/perl/ch-1.pl b/challenge-349/mahnkong/perl/ch-1.pl new file mode 100644 index 0000000000..b375fc2ee1 --- /dev/null +++ b/challenge-349/mahnkong/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +sub run($str) { + my $longest = 0; + while ($str =~ /(.)\1*/g) { + $longest = length($&) if length($&) > $longest; + } + return $longest; +} + +is(run("textbook"), 2, "Example 1"); +is(run("aaaaa"), 5, "Example 2"); +is(run("hoorayyy"), 3, "Example 3"); +is(run("x"), 1, "Example 4"); +is(run("aabcccddeeffffghijjk"), 4, "Example 5"); diff --git a/challenge-349/mahnkong/perl/ch-2.pl b/challenge-349/mahnkong/perl/ch-2.pl new file mode 100644 index 0000000000..77fed13894 --- /dev/null +++ b/challenge-349/mahnkong/perl/ch-2.pl @@ -0,0 +1,36 @@ +use strict; +use warnings; +use feature 'signatures'; +use Test::More 'no_plan'; + +my $moves = { + 'R' => sub($point) { + $point->{'x'} += 1; + }, + 'L' => sub($point) { + $point->{'x'} -= 1; + }, + 'U' => sub($point) { + $point->{'y'} += 1; + }, + 'D' => sub($point) { + $point->{'y'} -= 1; + } +}; + +sub run($path) { + my $point = {'x' => 0, 'y' => 0}; + do { + my $operation = substr($path, 0, 1); + $path = substr($path, 1); + $moves->{$operation}->($point); + return 1 if $point->{'x'} == 0 && $point->{'y'} == 0; + } while(length($path)); + return 0; +} + +is(run("ULD"), 0, "Example 1"); +is(run("ULDR"), 1, "Example 2"); +is(run("UUURRRDDD"), 0, "Example 3"); +is(run("UURRRDDLLL"), 1, "Example 4"); +is(run("RRUULLDDRRUU"), 1, "Example 5"); |
