diff options
| author | Alex Mauney <mathmauney@gmail.com> | 2020-08-26 08:46:22 -0700 |
|---|---|---|
| committer | Alex Mauney <mathmauney@gmail.com> | 2020-08-26 08:46:22 -0700 |
| commit | adca3826177366da2dee60b9af3a703949b9a747 (patch) | |
| tree | abea107fdb138e097ffe99ef91dba99388869ec5 /challenge-075/mathmauney | |
| parent | 423b552b8547f4125749ae062ca5e73a64dbc931 (diff) | |
| download | perlweeklychallenge-club-adca3826177366da2dee60b9af3a703949b9a747.tar.gz perlweeklychallenge-club-adca3826177366da2dee60b9af3a703949b9a747.tar.bz2 perlweeklychallenge-club-adca3826177366da2dee60b9af3a703949b9a747.zip | |
Submit solutions
Diffstat (limited to 'challenge-075/mathmauney')
| -rw-r--r-- | challenge-075/mathmauney/README | 1 | ||||
| -rw-r--r-- | challenge-075/mathmauney/perl/ch-1.pl | 65 | ||||
| -rw-r--r-- | challenge-075/mathmauney/perl/ch-2.pl | 36 |
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-075/mathmauney/README b/challenge-075/mathmauney/README new file mode 100644 index 0000000000..9856a99b57 --- /dev/null +++ b/challenge-075/mathmauney/README @@ -0,0 +1 @@ +Solution by Alex Mauney (@mathmauney) diff --git a/challenge-075/mathmauney/perl/ch-1.pl b/challenge-075/mathmauney/perl/ch-1.pl new file mode 100644 index 0000000000..95498c6c71 --- /dev/null +++ b/challenge-075/mathmauney/perl/ch-1.pl @@ -0,0 +1,65 @@ +#Tries to solve https://perlweeklychallenge.org/blog/perl-weekly-challenge-075/ as perl coding practice. + +use strict; +use warnings; +use feature qw(say); +use utf8; +use Getopt::Long; +use List::Util qw(sum first max min); +use Data::Dumper; + +my $target; +my @coins; + +GetOptions("target=i", \$target, + "coins=i{1,}" ,\@coins); + +@coins = sort @coins; +my @test_array = ($coins[0]) x 1; + +my $running = 1; +my $solutions = 0; +while ($running) { + if (sum(@test_array) == $target) { + say @test_array; + ++$solutions; + } + if (max(@test_array) == min(@coins) and sum(@test_array) >= $target) { + $running = 0; + } + @test_array = increment_array(@test_array); +} +say $solutions; + +sub increment_array { + my @tarray = @_; + my $running = 1; + my $i = 0; + my $max_idx = scalar @coins - 1; + while ($running) { + my $idx; + $idx = first { $coins[$_] == $tarray[$i] } 0..$#coins; + if ($idx < $max_idx) { + $tarray[$i] = $coins[$idx+1]; + $running = 0; + } elsif ($i+1 == scalar @tarray){ + push @tarray, 1; + @tarray = ($coins[0]) x @tarray; + $running = 0; + } else { + $tarray[$i] = $coins[0]; + } + ++$i; + if ($running == 0) { + my $max = scalar @tarray; + foreach (0..$max-2) { + if ($tarray[$_] < $tarray[$_+1]) { + $i = 0; + $running =1; + last; + } + } + } + } + return @tarray; +} diff --git a/challenge-075/mathmauney/perl/ch-2.pl b/challenge-075/mathmauney/perl/ch-2.pl new file mode 100644 index 0000000000..1f2c8cfe78 --- /dev/null +++ b/challenge-075/mathmauney/perl/ch-2.pl @@ -0,0 +1,36 @@ +#Tries to solve https://perlweeklychallenge.org/blog/perl-weekly-challenge-075/ as perl coding practice. + +use strict; +use warnings; +use feature qw(say); +use utf8; +use Getopt::Long; +use List::Util qw(sum first max min); +use Data::Dumper; + +my @hist; + +GetOptions("histogram=i{1,}", \@hist); + +my $count = 1; +my $max = 0; + +while (max(@hist) > 0) { + my @positives = map {$_ > 0} @hist; + my $l = 0; + my $maxl =0; + foreach (@positives) { + if ($_) { + ++$l; + } else { + $maxl = $l if $l > $maxl; + $l = 0; + } + } + $maxl = $l if $l > $maxl; + my $rect = $maxl * $count; + $max = $rect if $rect > $max; + @hist = map {$_ - 1} @hist; + ++$count +} +say $max; |
