diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-10 21:45:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-10 21:45:50 +0100 |
| commit | c09668092e28422d67258b8c86e4c92ff3a8ad1b (patch) | |
| tree | bdb53f21fcbefd4ef3985b2d979c9d595fc9b7d4 | |
| parent | 33de1dbe5885dd676005312d711f125e755e0186 (diff) | |
| parent | 61e74119914e237566a814ea4fbe411a69ab0a5b (diff) | |
| download | perlweeklychallenge-club-c09668092e28422d67258b8c86e4c92ff3a8ad1b.tar.gz perlweeklychallenge-club-c09668092e28422d67258b8c86e4c92ff3a8ad1b.tar.bz2 perlweeklychallenge-club-c09668092e28422d67258b8c86e4c92ff3a8ad1b.zip | |
Merge pull request #10406 from jaldhar/challenge-270
Challenge 270 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-270/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-270/jaldhar-h-vyas/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-270/jaldhar-h-vyas/perl/ch-2.pl | 55 | ||||
| -rwxr-xr-x | challenge-270/jaldhar-h-vyas/raku/ch-1.raku | 20 | ||||
| -rwxr-xr-x | challenge-270/jaldhar-h-vyas/raku/ch-2.raku | 28 |
5 files changed, 130 insertions, 0 deletions
diff --git a/challenge-270/jaldhar-h-vyas/blog.txt b/challenge-270/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..637f2af33b --- /dev/null +++ b/challenge-270/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2024/07/perl_weekly_challenge_week_270.html diff --git a/challenge-270/jaldhar-h-vyas/perl/ch-1.pl b/challenge-270/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..054e06ba82 --- /dev/null +++ b/challenge-270/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use v5.38; + +sub sum { + my $total; + for my $n (@_) { + $total += $n; + } + + return $total; +} + +my @matrix = map { [split /\s+/, $_] } @ARGV; +my $specials = 0; + +for my $row (keys @matrix) { + for my $col (keys @{$matrix[$row]}) { + if (($matrix[$row]->[$col] == 1) && + (sum(@{$matrix[$row]}) == 1) && + (sum(map { $matrix[$_]->[$col] } keys @matrix) == 1)) { + $specials++; + } + } +} + +say $specials;
\ No newline at end of file diff --git a/challenge-270/jaldhar-h-vyas/perl/ch-2.pl b/challenge-270/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..9ce546dc00 --- /dev/null +++ b/challenge-270/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +use v5.38; + +sub max(@arr) { + my $highest = '-inf'; + for my $i (@arr) { + if ($i > $highest) { + $highest = $i; + } + } + + return $highest; +} + +sub pick($count, @arr) { + my %results; + my $picked = 0; + + while ($picked < $count) { + my $random = $arr[int(rand(scalar @arr))]; + unless (exists $results{$random}) { + $results{$random} = 1; + $picked++; + } + } + + return wantarray ? keys %results : [ keys %results ]; +} + +my ($x, $y, @ints) = @ARGV; + +my $max = max(@ints); +my @unequals = @ints; +my $cost = 0; + +while(1) { + @unequals = grep { $_ != $max } @unequals; + my $remaining = scalar @unequals; + + if ($remaining > 1) { + for my $pick (pick(2, keys @unequals)) { + $unequals[$pick]++; + } + $cost += $y; + + } elsif ($remaining == 1) { + $unequals[0]++; + $cost += $x; + + } else { + last; + } +} + +say $cost; diff --git a/challenge-270/jaldhar-h-vyas/raku/ch-1.raku b/challenge-270/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..e9dfbd92ed --- /dev/null +++ b/challenge-270/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,20 @@ +#!/usr/bin/raku + +sub MAIN( + *@args +) { + my @matrix = @args.map({ [$_.words] }); + my $specials = 0; + + for @matrix.keys -> $row { + for @matrix[$row].keys -> $col { + if @matrix[$row;$col] == 1 && + @matrix[$row;*].sum == 1 && + @matrix[*;$col].sum == 1 { + $specials++; + } + } + } + + say $specials; +}
\ No newline at end of file diff --git a/challenge-270/jaldhar-h-vyas/raku/ch-2.raku b/challenge-270/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..a8c7891924 --- /dev/null +++ b/challenge-270/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/raku + +sub MAIN( + $x, $y, *@ints +) { + my $max = @ints.max; + my @unequals = @ints; + my $cost = 0; + + loop { + @unequals = @unequals.grep({ $_ != $max }); + my $remaining = @unequals.elems; + + if $remaining > 1 { + @unequals[@unequals.keys.pick(2)]ยป++; + $cost += $y; + + } elsif $remaining == 1 { + @unequals[0]++; + $cost += $x; + + } else { + last; + } + } + + say $cost; +}
\ No newline at end of file |
