aboutsummaryrefslogtreecommitdiff
path: root/challenge-152
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-19 10:57:22 +0000
committerGitHub <noreply@github.com>2022-02-19 10:57:22 +0000
commitcf38ab6faa9befb1deb2d07f9fd2c473152f8ed3 (patch)
tree4b927a46666ed4bc3ae6ab1b4bb1a6449ab57041 /challenge-152
parent293046d036027cf0d949f90594f0aa9266dae57c (diff)
parentda3a3bb9fbf381ffc7917ae9d8bbd864f728c0f9 (diff)
downloadperlweeklychallenge-club-cf38ab6faa9befb1deb2d07f9fd2c473152f8ed3.tar.gz
perlweeklychallenge-club-cf38ab6faa9befb1deb2d07f9fd2c473152f8ed3.tar.bz2
perlweeklychallenge-club-cf38ab6faa9befb1deb2d07f9fd2c473152f8ed3.zip
Merge pull request #5675 from pokgopun/new-branch
fix bug on parsing cmd args to rectangle's array, revise codes to dyn…
Diffstat (limited to 'challenge-152')
-rw-r--r--challenge-152/pokgopun/perl/ch-2.pl97
1 files changed, 64 insertions, 33 deletions
diff --git a/challenge-152/pokgopun/perl/ch-2.pl b/challenge-152/pokgopun/perl/ch-2.pl
index 00d2d57bf9..9cacf7c8a0 100644
--- a/challenge-152/pokgopun/perl/ch-2.pl
+++ b/challenge-152/pokgopun/perl/ch-2.pl
@@ -2,10 +2,10 @@
###
### Example usage: perl %script% -3,1 1,3 -1,-3 2,2
###
-### Input: Rectangle 1 => (-1,-3), (2,2)
-### Rectangle 2 => (-3,1), (1,3)
+### Input: Rectangle 1 => (-3,-1), (1,3)
+### Rectangle 2 => (-1,-3), (2,2)
###
-### Output: 21
+### Output: 25
##
#
use strict;
@@ -16,56 +16,87 @@ use Data::Dumper;
### For example "rec1_xl,rec1_yl" "rec1_xh,rec1_yh" "rec2_xl,rec2_yl" "rec2_xh,rec2_yh"
### Parse arguments and assign to array which is in turn used to create data structure to href by the order of labes
-sub recArea {
- my @val = ( map{@$_} map{@$_} @{shift @_} );
+sub recCov {
+ ### get array of rectangle's coordinates
+ my @rec = @{shift @_};
+ ### get debug toggle
my $debug = shift;
+ ### every rectangle get an id start from 1
+ my @rec_id = 1..@rec;
+ ### every rectangle's coordinate gets a position id, this will be 0 and 1 which are bottom left and top right for 2D rectangle
+ my @pos_id = 0..$#{$rec[0]};
+ ### every axis in a coordinate, this will be x and y for 2D rectangle
+ my @axis = @{["x".."z"]}[0..$#{$rec[0]->[0]}];
+ ### extract all the numbers from array of rectangle's cooridate, this will be converted to hash using rec_id, pos_id and axis_id
+ my @val = ( map{@$_} map{@$_} @rec );
my $val = {};
-
- foreach ( map{ [ $_ =~ /(\w+)/g ] } qw/r-1-x-l r-1-y-l r-1-x-h r-1-y-h r-2-x-l r-2-y-l r-2-x-h r-2-y-h/ ){
- $val->{$_->[0]}->{$_->[1]}->{$_->[2]}->{$_->[3]} = shift @val;
+ foreach my $rec_id (@rec_id){
+ foreach my $pos_id (@pos_id){
+ foreach my $axis (@axis){
+ push @{$val->{r}->{$rec_id}->{$axis}}, shift @val;
+ }
+ }
+ $val->{r}->{$rec_id}->{c} = eval(join( "*", map{ "abs(".join( "-", @{$val->{r}->{$rec_id}->{$_}} ).")" } @axis ));
}
-
- ### Calculate and add width,height,area of both rectangles
-
- my $rec = $val->{r};
- foreach my $r ( keys %$rec ){
- foreach ( map{ [ $_ =~ /(\w+)/g ] } qw/W-x H-y/ ){
- $rec->{$r}->{$_->[0]} = $rec->{$r}->{$_->[1]}->{h} - $rec->{$r}->{$_->[1]}->{l};
+ sub cTree {
+ my($c,$n,$e,$res) = @_;
+ if ( @$c == $n || @$c + @$e == $n ) {
+ my @res = @{[@$c,@$e]}[0..$n-1];
+ if ($res) {
+ push @$res, \@res;
+ } else {
+ printf "%s\n", join(", ",@res);
+ }
+ } else {
+ {
+ my $ct = [@$c,@{$e}[0]];
+ shift @$e if @$e;
+ &cTree($ct,$n,[@$e],$res);
+ redo if @$ct + @$e > $n;
+ }
}
- $rec->{$r}->{A} = $rec->{$r}->{W} * $rec->{$r}->{H};
}
-
- ### Create and add overlap value between both rectangles for x and y axis
- foreach ( qw/x y/ ) {
- my ($r1_l, $r1_h, $r2_l, $r2_h) = ($rec->{1}->{$_}->{l}, $rec->{1}->{$_}->{h}, $rec->{2}->{$_}->{l}, $rec->{2}->{$_}->{h});
- $val->{o}->{$_} = $r1_l >= $r2_h || $r2_l >= $r1_h ? 0 :
- $r1_h <= $r2_h && $r1_l >= $r2_l ? $r1_h - $r1_l :
- $r1_h >= $r2_h && $r1_l <= $r2_l ? $r2_h - $r2_l :
- $r1_h > $r2_h && $r1_l > $r2_l ? $r2_h - $r1_l :
- $r1_h < $r2_h && $r1_l < $r2_l ? $r1_h - $r2_l : undef;
+ my $o_rec = [];
+ &cTree([],2,[@rec_id],$o_rec);
+ foreach my $op (@$o_rec) {
+ my $o_pair = join("_",@$op);
+ my $ra = $val->{r}->{$op->[0]};
+ my $rb = $val->{r}->{$op->[1]};
+ foreach my $axis (@axis) {
+ my $ra0 = $ra->{$axis}->[0];
+ my $ra1 = $ra->{$axis}->[-1];
+ my $rb0 = $rb->{$axis}->[0];
+ my $rb1 = $rb->{$axis}->[-1];
+ $val->{o}->{$o_pair}->{$axis} = $ra0 >= $rb1 || $rb0 >= $ra1 ? 0 :
+ $ra1 <= $rb1 && $ra0 >= $rb0 ? $ra1 - $ra0 :
+ $ra1 >= $rb1 && $ra0 <= $rb0 ? $rb1 - $rb0 :
+ $ra1 > $rb1 && $ra0 > $rb0 ? $rb1 - $ra0 :
+ $ra1 < $rb1 && $ra0 < $rb0 ? $ra1 - $rb0 : undef;
+ }
+ $val->{o}->{$o_pair}->{c} = eval(join("*",values %{$val->{o}->{$o_pair}}));
}
-
{
last unless $debug;
print Dumper $val;
}
- return $rec->{1}->{A} + $rec->{2}->{A} - ( $val->{o}->{x} * $val->{o}->{y} );
+ my $sum_c = eval(join( "+", map{ $val->{r}->{$_}->{c} } @rec_id));
+ my $sum_o = eval(join( "+", map{ $val->{o}->{join("_",@$_)}->{c} } @$o_rec ));
+ return $sum_c - $sum_o;
}
my @sample;
{
last unless @ARGV;
- my $argv = {@ARGV};
my $sample;
- while (my($bl,$tr)=each%$argv){
- push @$sample, [ [$bl=~/(-?\d+)/g], [$tr=~/(-?\d+)/g] ];
- }
+ while (@ARGV) {
+ push @$sample, [ map{ [$_ =~ /(-?\d+)/g] } splice(@ARGV,0,2)];
+ }
push @sample,$sample;
}
{
last if @sample;
push @sample, [ [[-1,0],[2,2]], [[0,-1],[4,4]] ];
- push @sample, [ [[-3,1],[1,3]], [[-1,-3],[2,2]] ];
+ push @sample, [ [[-3,-1],[1,3]], [[-1,-3],[2,2]] ];
}
foreach (@sample) {
@@ -73,6 +104,6 @@ foreach (@sample) {
foreach (@$_) {
printf "%s Rectangle $i => %s\n", $i++==1 ? "Input:" : " ", join(", ", map{"(".join(",",@$_).")"} @$_);
}
- printf "\nOutput: %s\n\n", recArea($_);
+ printf "\nOutput: %s\n\n", recCov($_);
}