aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-28 12:58:08 +0000
committerGitHub <noreply@github.com>2020-03-28 12:58:08 +0000
commit9c107ce640fa43141b5bd09ca3634d561ed7bc5e (patch)
tree929ca7f362bca6371153b9c18d4683b5bc81ae78
parent8243f68d8958050b5c6b9ec801e32348fcc3d471 (diff)
parent5e2d95767d6dc6b2cf97135f6a4fca6be54e0b06 (diff)
downloadperlweeklychallenge-club-9c107ce640fa43141b5bd09ca3634d561ed7bc5e.tar.gz
perlweeklychallenge-club-9c107ce640fa43141b5bd09ca3634d561ed7bc5e.tar.bz2
perlweeklychallenge-club-9c107ce640fa43141b5bd09ca3634d561ed7bc5e.zip
Merge pull request #1474 from jaldhar/challenge-052
Challenge 52 by Jaldhar H. Vyas
-rw-r--r--challenge-052/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-052/jaldhar-h-vyas/perl/ch-1.pl11
-rwxr-xr-xchallenge-052/jaldhar-h-vyas/perl/ch-2.pl52
-rwxr-xr-xchallenge-052/jaldhar-h-vyas/raku/ch-1.p67
-rwxr-xr-xchallenge-052/jaldhar-h-vyas/raku/ch-2.p641
5 files changed, 112 insertions, 0 deletions
diff --git a/challenge-052/jaldhar-h-vyas/blog.txt b/challenge-052/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..012067b856
--- /dev/null
+++ b/challenge-052/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2020/03/perl_weekly_challenge_week_52.html
diff --git a/challenge-052/jaldhar-h-vyas/perl/ch-1.pl b/challenge-052/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..29ec31706e
--- /dev/null
+++ b/challenge-052/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+say join q{ }, grep {
+ my @digits = split //;
+
+ abs($digits[0] - $digits[1]) == 1 && abs($digits[1] - $digits[2]) == 1;
+} 100 .. 999;
+
diff --git a/challenge-052/jaldhar-h-vyas/perl/ch-2.pl b/challenge-052/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..596639fed5
--- /dev/null
+++ b/challenge-052/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+sub sum {
+ my $total = 0;
+ for my $elem (@{ $_[0] }) {
+ $total += $elem;
+ }
+
+ return $total;
+}
+
+sub minmax {
+ my @coins = @{ $_[0] };
+
+ return sum(\@coins[1 .. -1]) > sum(\@coins[0 .. -2]);
+}
+
+sub run {
+ my ($playerTurn) = @_;
+ my @coins = (100, 50, 1, 10, 5, 20, 200, 2);
+ my $playerAmount = 0;
+ my $computerAmount = 0;
+
+ while (scalar @coins) {
+ my $amount = minmax(\@coins) ? shift @coins : pop @coins;
+
+ if ($playerTurn) {
+ $playerAmount += $amount;
+ $playerTurn = undef;
+ } else {
+ $computerAmount += $amount;
+ $playerTurn = 1;
+ }
+ }
+
+ if ($playerAmount > $computerAmount) {
+ return (1, $playerAmount / 100);
+ } else {
+ return (undef, $computerAmount / 100);
+ }
+}
+
+say 'Assuming both take the best coin...';
+for my $playerTurn (1, undef) {
+ print 'If the ', ($playerTurn ? 'player' : 'computer'), ' goes first, ';
+ my ($winner, $amount) = run($playerTurn);
+ print 'the ', ($winner ? 'player' : 'computer'), ' wins with £',
+ $amount, ".\n";
+} \ No newline at end of file
diff --git a/challenge-052/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-052/jaldhar-h-vyas/raku/ch-1.p6
new file mode 100755
index 0000000000..fb6fab5324
--- /dev/null
+++ b/challenge-052/jaldhar-h-vyas/raku/ch-1.p6
@@ -0,0 +1,7 @@
+#!/usr/bin/perl6
+
+(100 .. 999).grep({
+ my @digits = $_.comb;
+ abs(@digits[0] - @digits[1]) == 1 && abs(@digits[1] - @digits[2]) == 1;
+}).join(q{ }).say;
+
diff --git a/challenge-052/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-052/jaldhar-h-vyas/raku/ch-2.p6
new file mode 100755
index 0000000000..44f88f6532
--- /dev/null
+++ b/challenge-052/jaldhar-h-vyas/raku/ch-2.p6
@@ -0,0 +1,41 @@
+#!/usr/bin/perl6
+
+sub minmax(@coins) {
+ return [+] @coins[1 .. *-1] > [+] @coins[0 .. *-2];
+}
+
+sub run(Bool $playerTurn_) {
+ my $playerTurn = $playerTurn_;
+ my @coins = (100, 50, 1, 10, 5, 20, 200, 2);
+ my $playerAmount = 0;
+ my $computerAmount = 0;
+
+ while @coins.elems {
+ my $amount = minmax(@coins) ?? @coins.shift !! @coins.pop;
+
+ if ($playerTurn) {
+ $playerAmount += $amount;
+ $playerTurn = False;
+ } else {
+ $computerAmount += $amount;
+ $playerTurn = True;
+ }
+ }
+
+ if ($playerAmount > $computerAmount) {
+ return (True, $playerAmount / 100);
+ } else {
+ return (False, $computerAmount / 100);
+ }
+}
+
+multi sub MAIN() {
+ say 'Assuming both take the best coin...';
+ for (True, False) -> $playerTurn {
+ print 'If the ', ($playerTurn ?? 'player' !! 'computer'),
+ ' goes first, ';
+ my ($winner, $amount) = run($playerTurn);
+ print 'the ', ($winner ?? 'player' !! 'computer'), ' wins with £',
+ $amount, ".\n";
+ }
+} \ No newline at end of file