diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-12 20:18:34 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-12 20:18:34 +0100 |
| commit | b7027b9a838c13ab54de3baa7ca520cd33219763 (patch) | |
| tree | 53a087c79a3fdde2bf63d326f5bc139a5027eafb /challenge-077 | |
| parent | b5126aad694761aee94fcfeb8ab0d3c8a395ff1c (diff) | |
| download | perlweeklychallenge-club-b7027b9a838c13ab54de3baa7ca520cd33219763.tar.gz perlweeklychallenge-club-b7027b9a838c13ab54de3baa7ca520cd33219763.tar.bz2 perlweeklychallenge-club-b7027b9a838c13ab54de3baa7ca520cd33219763.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-077')
| -rw-r--r-- | challenge-077/pete-houston/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-077/pete-houston/perl/ch-2.pl | 70 |
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-077/pete-houston/perl/ch-1.pl b/challenge-077/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..8e31ef75df --- /dev/null +++ b/challenge-077/pete-houston/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7701.pl +# +# USAGE: ./7701.pl N +# +# DESCRIPTION: Outputs all sums of unique Fibonacci numbers to make N +# +# REQUIREMENTS: Algorithm::Knapsack, Params::Util, List::Util +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 07/09/20 +#=============================================================================== + +use strict; +use warnings; +use Algorithm::Knapsack; +use Params::Util '_POSINT'; +use List::Util 'sum'; + +# Validate input +my $tot = shift; +die "Argument must be a whole number.\n" unless _POSINT $tot; + +# Construct a big enough Fibonacci sequence. No duplicates! +my @fib = (1, 2); +push @fib, $fib[-1] + $fib[-2] while $fib[-1] + $fib[-2] <= $tot; + +# Process +my $sack = Algorithm::Knapsack->new ( + capacity => $tot, + weights => \@fib, +); +$sack->compute; + +# Output +my $combos = 0; +for my $fit ($sack->solutions) { + next unless sum (@fib[@$fit]) == $tot; + print join (' + ', @fib[@$fit]) . " = $tot\n"; + $combos++; +} +print "0\n" unless $combos; diff --git a/challenge-077/pete-houston/perl/ch-2.pl b/challenge-077/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..bda3c2a0ba --- /dev/null +++ b/challenge-077/pete-houston/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7702.pl +# +# USAGE: ./7702.pl GRIDFILE +# +# DESCRIPTION: Find Xs with no X neighbours +# +# REQUIREMENTS: Path::Tiny, Lingua::EN::Inflexion, Lingua::EN::Nums2Words +# NOTES: In addition to the number found, also prints locations +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 12/09/20 +#=============================================================================== + +use strict; +use warnings; +use autodie; + +use Path::Tiny 'path'; +use Lingua::EN::Inflexion; + +my @grid = read_grid (shift); +my @xmess = find_solo_x (@grid); +print scalar (@xmess), "\n", @xmess; + +sub read_grid { + my @rows = path(shift)->lines ({chomp => 1}); + tr/OX//dc, $_ = [ split //, $_ ] for @rows; + return @rows; +} + +sub find_solo_x { + my @grid = @_; + my $maxrow = $#grid; + my $maxcol = $#{$grid[0]}; + die "Grid is not rectangular.\n" if grep { $#$_ != $maxcol } @grid; + my @solos; + for my $r (0 .. $maxrow) { + for my $c (0 .. $maxcol) { + next unless $grid[$r][$c] eq 'X'; + push @solos, mess ($r, $c, $#solos) + if all_os_around (\@grid, $r, $c); + } + } + return @solos; +} + +sub all_os_around { + my ($grid, $r, $c) = @_; + for my $i ($r - 1 .. $r + 1) { + next unless $i >= 0 && defined $grid->[$i]; + for my $j ($c - 1 .. $c + 1) { + next unless $j >= 0 && defined $grid->[$i][$j]; + next if $i == $r && $j == $c; + return if $grid->[$i][$j] eq 'X'; + } + } + return 1; +} + +sub mess { + my ($r, $c, $tot) = @_; + $tot += 2; + $r += 1; + $c += 1; + return ucfirst inflect ("<#ow:$tot> X found at Row $r Col $c.\n"); +} |
