diff options
| -rw-r--r-- | challenge-083/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-083/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-083/sgreen/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-083/sgreen/perl/ch-2.pl | 51 |
4 files changed, 73 insertions, 2 deletions
diff --git a/challenge-083/sgreen/README.md b/challenge-083/sgreen/README.md index 669247f1fc..4337d62b73 100644 --- a/challenge-083/sgreen/README.md +++ b/challenge-083/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 082 +# The Weekly Challenge 083 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-082-3a9d) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-083-4ga6) diff --git a/challenge-083/sgreen/blog.txt b/challenge-083/sgreen/blog.txt new file mode 100644 index 0000000000..337dd4ea24 --- /dev/null +++ b/challenge-083/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-083-4ga6 diff --git a/challenge-083/sgreen/perl/ch-1.pl b/challenge-083/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..11b736a9a5 --- /dev/null +++ b/challenge-083/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); + +sub main { + my $string = shift; + + my ($middle_words) = ( $string =~ /^\s*\S+?\s+(.*)\s+\S+\s*$/ ); + die "You must enter a string with at least three words\n" + unless $middle_words; + + # Remove ant whitespace + $middle_words =~ s/\s//g; + say length $middle_words; +} + +main(@ARGV); diff --git a/challenge-083/sgreen/perl/ch-2.pl b/challenge-083/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..e2454ff9c1 --- /dev/null +++ b/challenge-083/sgreen/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(say); + +use List::Util qw(sum uniq); + +sub main { + my @numbers = @_; + my $items = scalar(@numbers); + + # Sanity check + die "You must specify at least one value" unless scalar(@numbers); + foreach my $n (@numbers) { + die "The value '$n' is not a positive number\n" + unless $n =~ /^[1-9][0-9]*$/; + } + + # Set up some variables. We start with the minimum sum being one + # more than the sum of the numbers. + my $minimum_sum = sum(@numbers) + 1; + my $minimum_flips = 0; + my @results = (); + + # We use a binary counter representing the binary values for each + # number. If true, we will flip the value. + foreach my $x ( 0 .. 2**$items - 2 ) { + my @combination = + map { $x & 2**$_ ? -$numbers[$_] : $numbers[$_] } 0 .. $#numbers; + my $flips = scalar( grep { $_ < 0 } @combination ); + my $sum = sum(@combination); + next if $sum < 0; + + if ( $sum < $minimum_sum + || ( $sum == $minimum_sum && $flips < $minimum_flips ) ) + { + # We have a new leading combination + $minimum_sum = $sum; + $minimum_flips = $flips; + @results = ( join ' ', sort { $a <=> $b } @combination ); + } + elsif ( $sum == $minimum_sum && $flips == $minimum_flips ) { + push @results, join ' ', sort { $a <=> $b } @combination; + } + } + + say "RESULT IS: $minimum_flips (", join( ', ', uniq(@results) ), ')'; +} + +main(@ARGV); |
