From 3294653313ce942a6a1c8677a7ec04ad1db0b438 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 23 Nov 2020 22:55:06 +1000 Subject: sgreen solution to challenge 088 --- challenge-088/sgreen/README.md | 4 +-- challenge-088/sgreen/blog.txt | 1 + challenge-088/sgreen/perl/ch-1.pl | 32 ++++++++++++++++++++ challenge-088/sgreen/perl/ch-2.pl | 64 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 challenge-088/sgreen/blog.txt create mode 100755 challenge-088/sgreen/perl/ch-1.pl create mode 100755 challenge-088/sgreen/perl/ch-2.pl diff --git a/challenge-088/sgreen/README.md b/challenge-088/sgreen/README.md index 0281dfc367..99bc7be7ad 100644 --- a/challenge-088/sgreen/README.md +++ b/challenge-088/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 087 +# The Weekly Challenge 088 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-087-6mjh) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-088-5c5f) diff --git a/challenge-088/sgreen/blog.txt b/challenge-088/sgreen/blog.txt new file mode 100644 index 0000000000..54598014fb --- /dev/null +++ b/challenge-088/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-088-5c5f diff --git a/challenge-088/sgreen/perl/ch-1.pl b/challenge-088/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..6d6827bdbe --- /dev/null +++ b/challenge-088/sgreen/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use feature 'say'; +use List::Util qw(product); + +sub main { + my @N = @_; + + # Check that we have integers + die "You must specify one or more integers\n" unless scalar(@N); + foreach (@N) { + die "The value '$_' does not appear to be a positive integer\n" + unless /^\d+$/; + } + + # Special case if there is only one number + if ( scalar(@N) == 1 ) { + say '0'; + return; + } + + # Calculate the product of all numbers + my $product = product(@N); + + # The solution for each number is product divided by the number + say join ', ', map { $product / $_ } @N; +} + +main(@ARGV); diff --git a/challenge-088/sgreen/perl/ch-2.pl b/challenge-088/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..b21117feeb --- /dev/null +++ b/challenge-088/sgreen/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my @array = (); + + # Process the input + foreach (@_) { + push @array, [/(\d+)/g]; + } + + # Sanity check + foreach my $row ( 1 .. $#array ) { + die "Each row must have the same number of colums\n" + if scalar( @{ $array[0] } ) != scalar( @{ $array[$row] } ); + } + + my $rows = scalar(@array); + my $cols = scalar( @{ $array[0] } ); + + # Right, down, left and up + my @directions = ( [ 0, 1 ], [ 1, 0 ], [ 0, -1 ], [ -1, 0 ] ); + + # Map out the values we've used + my @used = ( map { [ (0) x $cols ] } ( 1 .. $rows ) ); + + # We start at the top left, moving right + my $x = 0; + my $y = 0; + my $direction = 0; + my @solutions = (); + + # Loop until we've found all the numbers + while ( scalar(@solutions) < $rows * $cols ) { + push @solutions, $array[$x][$y]; + $used[$x][$y] = 1; + + my $next_x = $x + $directions[$direction][0]; + my $next_y = $y + $directions[$direction][1]; + + # If we've reached the bounds of our grid, or found a value + # we've already used, we need to switch direction + if ( $next_x == $cols + or $next_y == $rows + or $next_x < 0 + or $next_y < 0 + or $used[$next_x][$next_y] ) + { + $direction = ++$direction % 4; + $next_x = $x + $directions[$direction][0]; + $next_y = $y + $directions[$direction][1]; + } + + $x = $next_x; + $y = $next_y; + } + + say join ', ', @solutions; +} + +main(@ARGV); -- cgit