From cb80fee1fbc25e093a382be4376daa1f4b810f5e Mon Sep 17 00:00:00 2001 From: Andreas Mahnke Date: Mon, 24 Nov 2025 12:01:38 +0100 Subject: Challenge 349 --- challenge-349/mahnkong/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-349/mahnkong/perl/ch-2.pl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-349/mahnkong/perl/ch-1.pl create mode 100644 challenge-349/mahnkong/perl/ch-2.pl 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"); -- cgit