diff options
| author | Michael Manring <michael@manring> | 2022-02-16 18:58:01 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-02-17 17:05:21 +0700 |
| commit | 92162db7b33f3b6d15004fc84f30530c60a2a725 (patch) | |
| tree | c83e5f4361614482796f8b1acf9886965013d64a /challenge-152 | |
| parent | c62f025cf9513caec622588cb9126aaa481d0314 (diff) | |
| download | perlweeklychallenge-club-92162db7b33f3b6d15004fc84f30530c60a2a725.tar.gz perlweeklychallenge-club-92162db7b33f3b6d15004fc84f30530c60a2a725.tar.bz2 perlweeklychallenge-club-92162db7b33f3b6d15004fc84f30530c60a2a725.zip | |
Guest Contribution from PokGoPun
Diffstat (limited to 'challenge-152')
| -rw-r--r-- | challenge-152/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-152/pokgopun/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-152/pokgopun/perl/ch-2.pl | 78 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-152/pokgopun/README b/challenge-152/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-152/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-152/pokgopun/perl/ch-1.pl b/challenge-152/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..ec75bb2919 --- /dev/null +++ b/challenge-152/pokgopun/perl/ch-1.pl @@ -0,0 +1,18 @@ +### Input: $triangle = [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ] +## Example of usage: perl %scripl% 1 5,3 2,3,4 7,1,0,2 6,4,5,2,8 +# +use strict; +use warnings; +#use Data::Dumper; + +my $mins = ""; +while (@ARGV) { + my $min = [sort { $a <=> $b } split(/\D/,shift)]->[0]; + # print Dumper $min; + $mins .= "$min "; +} +chop $mins; +my $sum; +$sum += $_ foreach split(/ /,$mins); +$mins =~ s/ /+/g; +print "Min Sum Path = $mins = $sum\n"; diff --git a/challenge-152/pokgopun/perl/ch-2.pl b/challenge-152/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..00d2d57bf9 --- /dev/null +++ b/challenge-152/pokgopun/perl/ch-2.pl @@ -0,0 +1,78 @@ +### Providing no argument, the script will use sample data similar to the examples in the challenge's task +### +### 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) +### +### Output: 21 +## +# +use strict; +use warnings; +use Data::Dumper; + +### Take four arguments as lower (i.e. botton-left) and higher (i.e. top-right) coordinates of 1st and 2nd rectangles +### 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 @_} ); + my $debug = shift; + 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; + } + + ### 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}; + } + $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; + } + + { + last unless $debug; + print Dumper $val; + } + return $rec->{1}->{A} + $rec->{2}->{A} - ( $val->{o}->{x} * $val->{o}->{y} ); +} + +my @sample; +{ + last unless @ARGV; + my $argv = {@ARGV}; + my $sample; + while (my($bl,$tr)=each%$argv){ + push @$sample, [ [$bl=~/(-?\d+)/g], [$tr=~/(-?\d+)/g] ]; + } + 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]] ]; +} + +foreach (@sample) { + my $i = 1; + foreach (@$_) { + printf "%s Rectangle $i => %s\n", $i++==1 ? "Input:" : " ", join(", ", map{"(".join(",",@$_).")"} @$_); + } + printf "\nOutput: %s\n\n", recArea($_); +} + |
