diff options
| author | Simon Green <mail@simon.green> | 2022-02-19 22:05:53 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2022-02-19 22:05:53 +1100 |
| commit | b6862b3f5784018439b080ec6c5faf5ef7642dd9 (patch) | |
| tree | e61263a89e4c262d540d3816a8328dc3b0b48050 /challenge-152 | |
| parent | 83a60a3be16f74882801abfea7f376f320db2a5e (diff) | |
| download | perlweeklychallenge-club-b6862b3f5784018439b080ec6c5faf5ef7642dd9.tar.gz perlweeklychallenge-club-b6862b3f5784018439b080ec6c5faf5ef7642dd9.tar.bz2 perlweeklychallenge-club-b6862b3f5784018439b080ec6c5faf5ef7642dd9.zip | |
sgreen solutions to challenge 152
Diffstat (limited to 'challenge-152')
| -rw-r--r-- | challenge-152/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-152/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-152/sgreen/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-152/sgreen/perl/ch-2.pl | 65 | ||||
| -rwxr-xr-x | challenge-152/sgreen/python/ch-1.py | 14 | ||||
| -rwxr-xr-x | challenge-152/sgreen/python/ch-2.py | 49 |
6 files changed, 148 insertions, 2 deletions
diff --git a/challenge-152/sgreen/README.md b/challenge-152/sgreen/README.md index f6354625bc..f87b671575 100644 --- a/challenge-152/sgreen/README.md +++ b/challenge-152/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 151 +# The Weekly Challenge 152 -Solution by Simon Green. [Weekly Challenge 151](https://dev.to/simongreennet/weekly-challenge-151-2812) +Blog: [Triangles and Rectangles](https://dev.to/simongreennet/triangles-and-rectangles-45p8) diff --git a/challenge-152/sgreen/blog.txt b/challenge-152/sgreen/blog.txt new file mode 100644 index 0000000000..8311c45cd2 --- /dev/null +++ b/challenge-152/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/triangles-and-rectangles-45p8
\ No newline at end of file diff --git a/challenge-152/sgreen/perl/ch-1.pl b/challenge-152/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e6aecda67f --- /dev/null +++ b/challenge-152/sgreen/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use List::Util qw(min sum); +use JSON; + + +sub main { + # Read the input + my $triangle = decode_json(shift); + say sum(map { min @$_ } @$triangle); +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-152/sgreen/perl/ch-2.pl b/challenge-152/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..27f2ebd271 --- /dev/null +++ b/challenge-152/sgreen/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use List::Util qw(min max); + +package Rectangle; + +sub new { + my ( $class, $x1, $y1, $x2, $y2 ) = @_; + # Ensure x1 is the left side + if ( $x1 > $x2 ) { + ( $x1, $x2 ) = ( $x2, $x1 ); + } + # Ensure y1 is the bottom side + if ( $y1 > $y2 ) { + ( $y1, $y2 ) = ( $y2, $y1 ); + } + + my $self = { + x1 => $x1, + x2 => $x2, + y1 => $y1, + y2 => $y2, + }; + + bless $self, $class; + return $self; +} + +sub area { + my $self = shift; + + # Return the area of the rectangle + return ( $self->{x2} - $self->{x1} ) * ( $self->{y2} - $self->{y1} ); +} + +package main; + +sub main { + my $s = shift; + my @numbers = ( $s =~ m/(-?\d+)/g ); + + # Find out the combined area of the two rectangles + my $rect1 = Rectangle->new( @numbers[ 0 .. 3 ] ); + my $rect2 = Rectangle->new( @numbers[ 4 .. 7 ] ); + my $area = $rect1->area + $rect2->area; + + # Calculate any overlap + my $dx1 = max( $rect1->{x1}, $rect2->{x1} ); + my $dy1 = max( $rect1->{y1}, $rect2->{y1} ); + my $dx2 = min( $rect1->{x2}, $rect2->{x2} ); + my $dy2 = min( $rect1->{y2}, $rect2->{y2} ); + + # If there is, remove that from the calculations + if ( $dx1 < $dx2 and $dy1 < $dy2 ) { + my $d = Rectangle->new( $dx1, $dy1, $dx2, $dy2 ); + $area -= $d->area(); + } + + say $area; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-152/sgreen/python/ch-1.py b/challenge-152/sgreen/python/ch-1.py new file mode 100755 index 0000000000..42da600bc0 --- /dev/null +++ b/challenge-152/sgreen/python/ch-1.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import sys +import json + + +def main(json_string): + # Read the input + triangle = json.loads(json_string) + print(sum(map(lambda x: min(x), triangle))) + + +if __name__ == '__main__': + main(sys.argv[1]) diff --git a/challenge-152/sgreen/python/ch-2.py b/challenge-152/sgreen/python/ch-2.py new file mode 100755 index 0000000000..668bc38018 --- /dev/null +++ b/challenge-152/sgreen/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import re +import sys + + +class Rectangle: + def __init__(self, x1, y1, x2, y2): + # Ensure x1 is the left side + if (x1 > x2): + (x1, x2) = (x2, x1) + # Ensure y1 is the bottom side + if (y1 > y2): + (y1, y2) = (y2, y1) + + self.x1 = x1 + self.x2 = x2 + self.y1 = y1 + self.y2 = y2 + + def area(self): + # Return the area of the rectangle + return (self.x2-self.x1) * (self.y2 - self.y1) + + +def main(s): + numbers = list(map(int, re.findall(r'-?\d+', s))) + + # Find out the combined area of the two rectangles + rect1 = Rectangle(*numbers[0:4]) + rect2 = Rectangle(*numbers[4:8]) + area = rect1.area() + rect2.area() + + # Calculate any overlap + dx1 = max(rect1.x1, rect2.x1) + dy1 = max(rect1.y1, rect2.y1) + dx2 = min(rect1.x2, rect2.x2) + dy2 = min(rect1.y2, rect2.y2) + + # If there is, remove that from the calculations + if dx1 < dx2 and dy1 < dy2: + d = Rectangle(dx1, dy1, dx2, dy2) + area -= d.area() + + print(area) + + +if __name__ == '__main__': + main(sys.argv[1]) |
