aboutsummaryrefslogtreecommitdiff
path: root/challenge-152/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-02-21 07:50:14 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-02-21 07:50:14 +0000
commit4781e9fa75bb48704e4e480ffc97f4b2e4043d3f (patch)
tree4cb6e514bb60ddf33fffe6c3b60ee1c8d0bbcd5a /challenge-152/james-smith
parent557c74ebe992c09731a36aee2e607e189a7c41fe (diff)
downloadperlweeklychallenge-club-4781e9fa75bb48704e4e480ffc97f4b2e4043d3f.tar.gz
perlweeklychallenge-club-4781e9fa75bb48704e4e480ffc97f4b2e4043d3f.tar.bz2
perlweeklychallenge-club-4781e9fa75bb48704e4e480ffc97f4b2e4043d3f.zip
unravel array into more variables to avoid references
Diffstat (limited to 'challenge-152/james-smith')
-rw-r--r--challenge-152/james-smith/README.md26
-rw-r--r--challenge-152/james-smith/perl/ch-2.pl6
2 files changed, 16 insertions, 16 deletions
diff --git a/challenge-152/james-smith/README.md b/challenge-152/james-smith/README.md
index 9e00afbeab..c4e88353c8 100644
--- a/challenge-152/james-smith/README.md
+++ b/challenge-152/james-smith/README.md
@@ -136,23 +136,23 @@ This gives us the solution:
```perl
sub my_area {
- my ($l,$r,$L,$R) = @_; ## $l,$r are the bottom-left & top-right corners of rectangle 1
- ## $L,$R are the bottom-left & top-right corners of rectangle 2
+ ## Lower case letter are the co-ordinates of triangle one
+ ## Upper case letters are the co-ordinates of triangle two
- ## Compute 3 widths and heights...
+ my ($l,$b,$r,$t,$L,$B,$R,$T) = map { @{$_} } @_;
- my $w3 = ( my $w1 = $r->[0] - $l->[0] ) ## width rectangle 1
- + ( my $w2 = $R->[0] - $L->[0] ) ## width rectangle 2
- - ( $r->[0] > $R->[0] ? $r->[0] : $R->[0] ) ## right most point
- + ( $l->[0] < $L->[0] ? $l->[0] : $L->[0] ); ## left most point
- my $h3 = ( my $h1 = $r->[1] - $l->[1] ) ## height rectangle 1
- + ( my $h2 = $R->[1] - $L->[1] ) ## height rectangle 2
- - ( $r->[1] > $R->[1] ? $r->[1] : $R->[1] ) ## highest point
- + ( $l->[1] < $L->[1] ? $l->[1] : $L->[1] ); ## lowest point
+ ## The 3rd/4th brackets are the min/max of bounding box...
- ## Return result...
+ ## w1 - width 1st rect, w2 - width 2nd rect, w3 - width of overlap (if > 0)
+
+ my $w3 = (my $w1=$r-$l)+(my $w2=$R-$L)+($l<$L?$l:$L)-($r<$R?$R:$r);
+
+ ## h1 - height 1st rect, h2 - height 2nd rect, h3 - height of overlap (if > 0)
+
+ my $h3 = (my $h1=$t-$b)+(my $h2=$T-$B)+($b<$B?$b:$B)-($t<$T?$T:$t);
+
+ ## return result
$w1*$h1 + $w2*$h2 - ($w3>0 && $h3>0 && $w3*$h3);
}
-```
diff --git a/challenge-152/james-smith/perl/ch-2.pl b/challenge-152/james-smith/perl/ch-2.pl
index 05164f9308..4d8e56e0d3 100644
--- a/challenge-152/james-smith/perl/ch-2.pl
+++ b/challenge-152/james-smith/perl/ch-2.pl
@@ -20,9 +20,9 @@ is( my_area(@{$_->[0]}), $_->[1] ) foreach @TESTS;
done_testing();
sub my_area {
- my ($l,$r,$L,$R) = @_;
- my $w3 = (my $w1 = $r->[0]-$l->[0]) + (my $w2 = $R->[0]-$L->[0]) + ($l->[0]<$L->[0]?$l->[0]:$L->[0]) - ($r->[0]>$R->[0]?$r->[0]:$R->[0]);
- my $h3 = (my $h1 = $r->[1]-$l->[1]) + (my $h2 = $R->[1]-$L->[1]) + ($l->[1]<$L->[1]?$l->[1]:$L->[1]) - ($r->[1]>$R->[1]?$r->[1]:$R->[1]);
+ my ($l,$b,$r,$t,$L,$B,$R,$T) = map { @{$_} } @_;
+ my $w3 = (my $w1=$r-$l)+(my $w2=$R-$L)+($l<$L?$l:$L)-($r<$R?$R:$r);
+ my $h3 = (my $h1=$t-$b)+(my $h2=$T-$B)+($b<$B?$b:$B)-($t<$T?$T:$t);
$w1*$h1 + $w2*$h2 - ($w3>0 && $h3>0 && $w3*$h3);
}