From 98fdbf2adf6470fed09efb6c9facfb822caa865f Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Thu, 6 Nov 2025 12:07:44 +0000 Subject: Week 346 - (Magic) --- challenge-346/peter-campbell-smith/blog.txt | 1 + challenge-346/peter-campbell-smith/perl/ch-1.pl | 73 +++++++++++++++++++++++++ challenge-346/peter-campbell-smith/perl/ch-2.pl | 54 ++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 challenge-346/peter-campbell-smith/blog.txt create mode 100755 challenge-346/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-346/peter-campbell-smith/perl/ch-2.pl 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'); +} -- cgit