aboutsummaryrefslogtreecommitdiff
path: root/challenge-152
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-17 15:18:05 +0000
committerGitHub <noreply@github.com>2022-02-17 15:18:05 +0000
commit6f434d93da0acaf149d1b05dcb168b6be2641412 (patch)
treed825b424be0d27cbff2743792e0bfd6218551788 /challenge-152
parentabb930539f29df77a94ea318217ffa369b582823 (diff)
parent9c282eaff3094eae52c82e5f02586d6a973ccd86 (diff)
downloadperlweeklychallenge-club-6f434d93da0acaf149d1b05dcb168b6be2641412.tar.gz
perlweeklychallenge-club-6f434d93da0acaf149d1b05dcb168b6be2641412.tar.bz2
perlweeklychallenge-club-6f434d93da0acaf149d1b05dcb168b6be2641412.zip
Merge pull request #5665 from ccntrq/challenge-152
Add solutions for challenge 152
Diffstat (limited to 'challenge-152')
-rwxr-xr-xchallenge-152/alexander-pankoff/perl/ch-1.pl58
-rwxr-xr-xchallenge-152/alexander-pankoff/perl/ch-2.pl86
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-152/alexander-pankoff/perl/ch-1.pl b/challenge-152/alexander-pankoff/perl/ch-1.pl
new file mode 100755
index 0000000000..620b748894
--- /dev/null
+++ b/challenge-152/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw'say state signatures';
+no warnings qw'experimental::signatures';
+
+# TASK #1 › Triangle Sum Path
+# Submitted by: Mohammad S Anwar
+#
+# You are given a triangle array.
+#
+# Write a script to find the minimum sum path from top to bottom.
+# Example 1:
+#
+# Input: $triangle = [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]
+#
+# 1
+# 5 3
+# 2 3 4
+# 7 1 0 2
+# 6 4 5 2 8
+#
+# Output: 8
+#
+# Minimum Sum Path = 1 + 3 + 2 + 0 + 2 => 8
+#
+# Example 2:
+#
+# Input: $triangle = [ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ]
+#
+# 5
+# 2 3
+# 4 1 5
+# 0 1 2 3
+# 7 2 4 1 9
+#
+# Output: 9
+#
+# Minimum Sum Path = 5 + 2 + 1 + 0 + 1 => 9
+
+use List::Util qw(min sum0);
+
+run() unless caller();
+
+sub run() {
+ my $example1 =
+ [ [1], [ 5, 3 ], [ 2, 3, 4 ], [ 7, 1, 0, 2 ], [ 6, 4, 5, 2, 8 ] ];
+ my $example2 =
+ [ [5], [ 2, 3 ], [ 4, 1, 5 ], [ 0, 1, 2, 3 ], [ 7, 2, 4, 1, 9 ] ];
+ say "Running example 1";
+ say minimum_triangle_sum_path($example1);
+ say "Running example 2";
+ say minimum_triangle_sum_path($example2);
+}
+
+sub minimum_triangle_sum_path($triangle) {
+ return sum0( map { min(@$_) } @$triangle );
+}
diff --git a/challenge-152/alexander-pankoff/perl/ch-2.pl b/challenge-152/alexander-pankoff/perl/ch-2.pl
new file mode 100755
index 0000000000..78ec3877a6
--- /dev/null
+++ b/challenge-152/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw'say state signatures';
+no warnings qw'experimental::signatures';
+
+# TASK #2 › Rectangle Area
+# Submitted by: Mohammad S Anwar
+#
+# You are given coordinates bottom-left and top-right corner of two rectangles in a 2D plane.
+#
+# Write a script to find the total area covered by the two rectangles.
+# Example 1:
+#
+# Input: Rectangle 1 => (-1,0), (2,2)
+# Rectangle 2 => (0,-1), (4,4)
+#
+# Output: 22
+#
+# Example 2:
+#
+# Input: Rectangle 1 => (-3,-1), (1,3)
+# Rectangle 2 => (-1,-3), (2,2)
+#
+# Output: 25
+
+use List::Util qw(min max);
+
+run() unless caller();
+
+sub run() {
+
+ say "Running example 1";
+ say rectangles_area( [ [ -1, 0 ], [ 2, 2 ] ], [ [ 0, -1 ], [ 4, 4 ] ] );
+ say "Running example 2";
+ say rectangles_area( [ [ -3, -1 ], [ 1, 3 ] ], [ [ -1, -3 ], [ 2, 2 ] ] );
+ say "Running example without overlap";
+ say rectangles_area( [ [ -1, 0 ], [ 0, 1 ] ], [ [ 1, 0 ], [ 2, 1 ] ] );
+}
+
+sub rectangles_area ( $rectangle1, $rectangle2 ) {
+ say "Rectangle 1 => " . show_rectangle_corners($rectangle1);
+ say "Rectangle 2 => " . show_rectangle_corners($rectangle2);
+
+ my $area1 = rectangle_area($rectangle1);
+ my $area2 = rectangle_area($rectangle2);
+
+ my $overlap_rectangle =
+ find_overlapping_rectangle( $rectangle1, $rectangle2 );
+
+ my $overlap_area = rectangle_area($overlap_rectangle);
+
+ return $area1 + $area2 - $overlap_area;
+}
+
+sub rectangle_area($rectangle) {
+ my ( $x1, $y1, $x2, $y2 ) = map { @$_ } @$rectangle;
+
+ my $x = abs( $x1 - $x2 );
+ my $y = abs( $y1 - $y2 );
+
+ return $x * $y;
+
+}
+
+sub find_overlapping_rectangle ( $rectangle1, $rectangle2 ) {
+ my ( $x11, $y11, $x12, $y12 ) = map { @$_ } @$rectangle1;
+ my ( $x21, $y21, $x22, $y22 ) = map { @$_ } @$rectangle2;
+
+ my $x1 = max( $x11, $x21 );
+ my $y1 = max( $y11, $y21 );
+
+ my $x2 = min( $x12, $x22 );
+ my $y2 = min( $y12, $y22 );
+
+ if ( $x1 > $x2 || $y1 > $y2 ) {
+ ## no overlap, return rectangle with empty area
+ return [ [ 0, 0 ], [ 0, 0 ] ];
+ }
+
+ return [ [ $x1, $y1 ], [ $x2, $y2 ] ];
+}
+
+sub show_rectangle_corners($rectangle) {
+ join( ', ', map { '(' . join( ',', @$_ ) . ')' } @$rectangle );
+}