diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-11 13:56:09 +0100 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-08-11 13:56:09 +0100 |
| commit | 032c2edb893353dad4fe841d8942e07750bcf29a (patch) | |
| tree | f5b38fbbe30f4fbfeddd2e03254557e8390440cc | |
| parent | 730e172acb159a2fc320a78a6250083328ef1067 (diff) | |
| download | perlweeklychallenge-club-032c2edb893353dad4fe841d8942e07750bcf29a.tar.gz perlweeklychallenge-club-032c2edb893353dad4fe841d8942e07750bcf29a.tar.bz2 perlweeklychallenge-club-032c2edb893353dad4fe841d8942e07750bcf29a.zip | |
Week 334 - A slice of New York
| -rw-r--r-- | challenge-334/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-334/peter-campbell-smith/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-334/peter-campbell-smith/perl/ch-2.pl | 44 |
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-334/peter-campbell-smith/blog.txt b/challenge-334/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..420352de78 --- /dev/null +++ b/challenge-334/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/334 diff --git a/challenge-334/peter-campbell-smith/perl/ch-1.pl b/challenge-334/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..ad8c80ea49 --- /dev/null +++ b/challenge-334/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-11 +use utf8; # Week 334 - task 1 - Range sum +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +range_sum([-2, 0, 3, -5, 2, -1], 0, 2); +range_sum([1, -2, 3, -4, 5], 1, 3); +range_sum([1, 0, 2, -1, 3], 3, 4); +range_sum([-5, 4, -3, 2, -1, 0], 0, 3); +range_sum([-1, 0, 2, -3, -2, 1], 0, 2); + +sub range_sum { + + my (@integers, $from, $to, $sum); + + # do it + @integers = @{$_[0]}; + $sum += $_ for @integers[$_[1] .. $_[2]]; + + say qq[\nInput: \@integers = (] . join(', ', @integers) . qq[), \$x = $_[1], \$y = $_[2]]; + say qq[Output: $sum]; +} + diff --git a/challenge-334/peter-campbell-smith/perl/ch-2.pl b/challenge-334/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..5ba58e7cb7 --- /dev/null +++ b/challenge-334/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-08-11 +use utf8; # Week 334 - task 2 - Nearest valid point +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +nearest_valid_point(3, 4, [[1, 2], [3, 1], [2, 4], [2, 3]]); +nearest_valid_point(2, 5, [[3, 4], [2, 3], [1, 5], [2, 5]]); +nearest_valid_point(1, 1, [[2, 2], [3, 3], [4, 4]]); +nearest_valid_point(0, 0, [[0, 1], [1, 0], [0, 2], [2, 0]]); +nearest_valid_point(5, 5, [[5, 6], [6, 5], [5, 4], [4, 5]]); + +sub nearest_valid_point { + + my ($x, $y, $px, $py, $point, $points, $best, $mhd, $winner, $point_list, $index); + + # initialise + ($x, $y, $points) = @_; + $best = 1e10; + + # loop over points + for $index (0 .. scalar(@$points) - 1) { + ($px, $py) = @{$points->[$index]}; + $point_list .= qq{[$px, $py], }; + + # check validity + if ($px == $x) { $mhd = abs($py - $y) } + elsif ($py == $y) { $mhd = abs($px - $x) } + else { next } + + # is this the winner? + if ($mhd < $best) { + $best = $mhd; + $winner = qq{index = $index, coords = [$px, $py], distance = $mhd}; + } + } + + say qq[\nInput: \$x = $x, \$y = $y, \@points = ] . substr($point_list, 0, -2); + say qq[Output: ] . (defined($winner) ? $winner : '-1'); +} |
