diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-24 12:41:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-24 12:41:43 +0100 |
| commit | badfecd49fcfc1aacd133eeb741dca2196f69299 (patch) | |
| tree | 5ae3b599c5029c3acab60c93db0166f84f4a28e1 | |
| parent | 69a43e4a8361d46c550842ed3cedc3fed019fe52 (diff) | |
| parent | cc143d9ea913268e25bf25baaf0a29a3fff92635 (diff) | |
| download | perlweeklychallenge-club-badfecd49fcfc1aacd133eeb741dca2196f69299.tar.gz perlweeklychallenge-club-badfecd49fcfc1aacd133eeb741dca2196f69299.tar.bz2 perlweeklychallenge-club-badfecd49fcfc1aacd133eeb741dca2196f69299.zip | |
Merge pull request #10139 from jacoby/master
DAJ 270
| -rw-r--r-- | challenge-270/dave-jacoby/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-270/dave-jacoby/perl/ch-2.pl | 50 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-270/dave-jacoby/perl/ch-1.pl b/challenge-270/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..6e771215b4 --- /dev/null +++ b/challenge-270/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = ( + + [ [ 1, 0, 0 ], [ 0, 0, 1 ], [ 1, 0, 0 ], ], + [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ], + ], + +); +for my $example (@examples) { + my $output = special_positions($example); + my $input = display_matrix( $example); +say <<"END"; + Input: \$matrix = + [ $input ] + Output: $output +END +} + +sub special_positions ($matrix) { + my $output = 0; +OUTER: for my $i ( 0 .. -1 + scalar keys $matrix->@* ) { + for my $j ( 0 .. -1 + scalar keys $matrix->[$i]->@* ) { + for my $x ( 0 .. -1 + scalar keys $matrix->[$i]->@* ) { + my $v = $matrix->[$i][$x]; + next OUTER if $v == 0 && $x == $j; + next OUTER if $v != 0 && $x != $j; + } + for my $y ( 0 .. -1 + scalar keys $matrix->@* ) { + my $v = $matrix->[$y][$j]; + next OUTER if $v == 0 && $y == $i; + next OUTER if $v != 0 && $y != $i; + } + $output++; + } + } + return $output; +} + +sub display_matrix ($matrix) { + return join ",\n ", + map { join ' ', '[', ( join ', ', $_->@* ), ']' } $matrix->@*; +} diff --git a/challenge-270/dave-jacoby/perl/ch-2.pl b/challenge-270/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c3c1c36776 --- /dev/null +++ b/challenge-270/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ first max }; + +my @examples = ( + + [ 3, 2, 4, 1 ], + [ 2, 1, 2, 3, 3, 3, 5 ], + [ 1, 3, 2, 3, 3, 3, 5 ], +); + +for my $example (@examples) { + my $output = distribute_elements( $example->@* ); + my ( $x, $y, @ints ) = $example->@*; + my $ints = join ', ', @ints; + + say <<"END"; + Input: \@ints = ($ints), \$x = $x, \$y = $y + Output: $output +END +} + +sub distribute_elements (@input) { + my ( $x, $y, @ints ) = @input; + my $max = max @ints; + my $count = scalar grep { $_ < $max } @ints; + my $cost = 0; + my $check = $x * 2 >= $y ? 1 : 0; + while ($count) { + if ( $check && $count > 1 ) { + my $f = first { $ints[$_] < $max } 0 .. -1 + scalar @ints; + my $s = + first { $ints[$_] < $max && $_ != $f } 0 .. -1 + scalar @ints; + $ints[$f]++; + $ints[$s]++; + $cost += $y; + } + else { + my $f = ( first { $ints[$_] < $max } 0 .. -1 + scalar @ints ); + $ints[$f]++; + $cost += $x; + } + $count = scalar grep { $_ < $max } @ints; + } + return $cost; +} |
