aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-11-06 12:07:44 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-11-06 12:07:44 +0000
commit98fdbf2adf6470fed09efb6c9facfb822caa865f (patch)
tree732b5b32d9c8633809436bc8fd16476d93a24622
parentd6d9df839741d01f80758e2acc9e6cc83ad5336a (diff)
downloadperlweeklychallenge-club-98fdbf2adf6470fed09efb6c9facfb822caa865f.tar.gz
perlweeklychallenge-club-98fdbf2adf6470fed09efb6c9facfb822caa865f.tar.bz2
perlweeklychallenge-club-98fdbf2adf6470fed09efb6c9facfb822caa865f.zip
Week 346 - (Magic)
-rw-r--r--challenge-346/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-346/peter-campbell-smith/perl/ch-1.pl73
-rwxr-xr-xchallenge-346/peter-campbell-smith/perl/ch-2.pl54
3 files changed, 128 insertions, 0 deletions
diff --git a/challenge-346/peter-campbell-smith/blog.txt b/challenge-346/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..17fe49a6f1
--- /dev/null
+++ b/challenge-346/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/346
diff --git a/challenge-346/peter-campbell-smith/perl/ch-1.pl b/challenge-346/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..c214be4077
--- /dev/null
+++ b/challenge-346/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-03
+use utf8; # Week 346 - task 1 - Longest parenthesis
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+longest_parenthesis('(()())');
+longest_parenthesis(')()())');
+longest_parenthesis('((()))()(((()');
+longest_parenthesis('))))((()(');
+longest_parenthesis('()(()');
+longest_parenthesis('))');
+longest_parenthesis('()()()()()');
+longest_parenthesis(')))())))');
+
+sub longest_parenthesis {
+
+ my (@parens, $best, $explain, $start, $i, $length,
+ $height, @opens, $this, %previous);
+
+ # initialise
+ @parens = split('', $_[0]);
+ @opens = ();
+ $best = $start = $height = 0;
+ $explain = '';
+
+ # loop over string
+ for $i (0 .. $#parens) {
+ $this = $parens[$i];
+
+ # found ( - record as start
+ if ($this eq '(') {
+ push(@opens, $i);
+ $height ++;
+
+ # found )
+ } elsif ($this eq ')' and $height > 0) {
+ $height --;
+
+ # ( and ) match
+ unless ($height < 0) {
+ $start = pop(@opens);
+
+ # check for this being the longest
+ $length = $i - $start + 1;
+ if (defined $previous{end} and $start == $previous{end} + 1) {
+ $length += $previous{length};
+ $start = $previous{start};
+ }
+
+ # record info
+ if ($length >= $best) {
+ $explain = '' if $length > $best;
+ $best = $length;
+ $explain .= qq[($start .. $i), ];
+ %previous = (start => $start, length => $length, end => $i);
+ }
+
+ # unmatched ) - forget all previous starts
+ } else {
+ @opens = ();
+ $start = $i + 1;
+ }
+ }
+ }
+
+ say qq[\nInput: '$_[0]'];
+ say qq[Output: ] . ($best > 0 ? qq[$best at ] . substr($explain, 0, -2) : 'none');
+}
diff --git a/challenge-346/peter-campbell-smith/perl/ch-2.pl b/challenge-346/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..cd4a0898cb
--- /dev/null
+++ b/challenge-346/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-03
+use utf8; # Week 346 - task 2 - Magic expression
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+use Algorithm::Combinatorics 'variations_with_repetition';
+
+magic_expression(123, 6);
+magic_expression(105, 5);
+magic_expression(232, 8);
+magic_expression(1234, 10);
+magic_expression(13579, 115);
+magic_expression(314159265, 868140);
+
+sub magic_expression {
+
+ my (@ints, $target, $gaps, @ops, $iterator, $p, $trial, $g, $value, $output);
+
+ # initialise
+ @ints = split('', $_[0]);
+ $target = $_[1];
+ $gaps = $#ints;
+ @ops = (' + ', ' - ', ' * ', '');
+ $output = '';
+
+ # loop over variations of @ops
+ $iterator = variations_with_repetition([0, 1, 2, 3], $gaps);
+ while ($p = $iterator->next) {
+
+ # create string of digits with every perm of @ops
+ $trial = '';
+ for $g (0 .. $gaps - 1) {
+ $trial .= $ints[$g] . $ops[$p->[$g]];
+ }
+ $trial .= $ints[$gaps];
+
+ # eliminate ones containing leadng 0s eg 05
+ next if $trial =~ m{( 0\d|\(0\d)};
+
+ # evaluate the string and see if it matches $target
+ $value = eval($trial);
+ next unless $value == $target;
+
+ # it does
+ $output .= qq[($trial), ];
+ }
+
+ say qq[\nInput: \$str = '$_[0]', \$target = $target];
+ say qq[Output: \$output = ] . ($output ? substr($output, 0, -2) : 'not possible');
+}