diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-18 00:04:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-18 00:04:43 +0100 |
| commit | a293518dd1989727b5f2fb742c36e9ff510c3fbe (patch) | |
| tree | 5e751fa36074bdb3c8ed6f1dee73216c4e9b4e16 | |
| parent | 1065e85061de1d09aba61d6bc102a3a94fe95ab7 (diff) | |
| parent | e5db02d3db8e2ce108adf12d621bd40345260a5f (diff) | |
| download | perlweeklychallenge-club-a293518dd1989727b5f2fb742c36e9ff510c3fbe.tar.gz perlweeklychallenge-club-a293518dd1989727b5f2fb742c36e9ff510c3fbe.tar.bz2 perlweeklychallenge-club-a293518dd1989727b5f2fb742c36e9ff510c3fbe.zip | |
Merge pull request #12529 from mattneleigh/pwc334
new file: challenge-334/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-334/mattneleigh/perl/ch-1.pl | 87 | ||||
| -rwxr-xr-x | challenge-334/mattneleigh/perl/ch-2.pl | 140 |
2 files changed, 227 insertions, 0 deletions
diff --git a/challenge-334/mattneleigh/perl/ch-1.pl b/challenge-334/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..22d9f94a48 --- /dev/null +++ b/challenge-334/mattneleigh/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @data_sets = ( + [ + [ -2, 0, 3, -5, 2, -1 ], + 0, + 2 + ], + [ + [ 1, -2, 3, -4, 5 ], + 1, + 3 + ], + [ + [ 1, 0, 2, -1, 3 ], + 3, + 4 + ], + [ + [ -5, 4, -3, 2, -1, 0 ], + 0, + 3 + ], + [ + [ -1, 0, 2, -3, -2, 1 ], + 0, + 2 + ] +); + +print("\n"); +foreach my $data_set (@data_sets){ + printf( + "Input: \@ints = (%s), \$x = %d, \$y = %d\nOutput: %d\n\n", + join(", ", @{$data_set->[0]}), + $data_set->[1], + $data_set->[2], + range_sum($data_set) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a list of integers and a pair of indices, compute the sum of the values +# from the list that lie between the given indices, inclusive +# Takes one argument: +# * A ref to a list that includes the list of integers and the indices over +# which to sum values (e.g. +# [ +# [ 1, 0, 2, -1, 3 ], +# 3, +# 4 +# ], +# ) +# Returns: +# * The sum of the values between the given indices, inclusive (e.g. 2) +################################################################################ +sub range_sum{ + + my $sum = 0; + + # Sum the values in an array slice corresponding + # to the given indices + foreach(@{$ARG[0][0]}[ $ARG[0][1] .. $ARG[0][2] ]){ + $sum += $_; + } + + return($sum); + +} + + + diff --git a/challenge-334/mattneleigh/perl/ch-2.pl b/challenge-334/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..ada68c5614 --- /dev/null +++ b/challenge-334/mattneleigh/perl/ch-2.pl @@ -0,0 +1,140 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @data_sets = ( + [ + [ [1, 2], [3, 1], [2, 4], [2, 3] ], + 3, + 4 + ], + [ + [ [3, 4], [2, 3], [1, 5], [2, 5] ], + 2, + 5 + ], + [ + [ [2, 2], [3, 3], [4, 4] ], + 1, + 1 + ], + [ + [ [0, 1], [1, 0], [0, 2], [2, 0] ], + 0, + 0 + ], + [ + [ [5, 6], [6, 5], [5, 4], [4, 5] ], + 5, + 5 + ] +); + +print("\n"); +foreach my $data_set (@data_sets){ + printf( + "Input: \$x = %d, \$y = %d, \@points (%s)\nOutput: %d\n\n", + $data_set->[1], + $data_set->[2], + join( + ", ", + map( + "[" . join(", ", @{$_}) . "]", + @{$data_set->[0]} + ) + ), + minimum_manhattan_distance($data_set) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given a current location on a grid, as well as a list of other points, +# determine which valid point has the minimum Manhattan distance from the +# current point; a point is valid if it has the same X or Y coordinate as the +# current location, and the Manhattan distance is defined as: +# +# $Dm = abs($Xcurr - $Xi) + abs($Ycurr - $Yi); +# +# where the points involved are the current point (Xcurr, Ycurr) and the point +# from the list is (Xi, Yi). The index of the point with the shortest +# Manhattan distance will be returned; if two points share the same distance, +# the index of the first one observed will be returned, while -1 will be +# returned if no valid point exists in the list. +# Takes one argument: +# * A ref to a data structure that contains the list of points to examine and +# the current set of coordinates (e.g. +# [ +# [ [3, 4], [2, 3], [1, 5], [2, 5] ], +# 2, +# 5 +# ] +# ) +# Returns: +# * The index of the point with the shortest Manhattan distance observed within +# the list; if there are multiple points with the same distance, the index +# returned will be that of the first such point observed (e.g. 3) +# * -1 if there are no valid points within the list +################################################################################ +sub minimum_manhattan_distance{ + my $point_data = shift(); + + my $distance; + my $min_distance; + my $min_index = undef; + + for my $i (0 .. $#{$point_data->[0]}){ + # Skip points that don't match at least one + # coordinate with the current location + next + unless( + ($point_data->[0][$i][0] == $point_data->[1]) + || + ($point_data->[0][$i][1] == $point_data->[2]) + ); + + $distance = + abs($point_data->[0][$i][0] - $point_data->[1]) + + + abs($point_data->[0][$i][1] - $point_data->[2]); + + # Store distance and index if this is the + # first or if it's less than a previously seen + # index; if this distance matches a previously + # seen example, it will be skipped + if( + !defined($min_index) + || + ($distance < $min_distance) + ){ + $min_distance = $distance; + $min_index = $i; + } + } + + # If valid point(s) were observed in the list, + # return the index of the minimum seen, + # otherwise return -1 + return( + defined($min_index) ? + $min_index + : + -1 + ); + +} + + + |
