aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-21 14:03:23 +0000
committerGitHub <noreply@github.com>2020-03-21 14:03:23 +0000
commite52d437514981abcaec01d95542bcfc682125ea1 (patch)
tree0f130fa065431a805fd48ac2d831befbcfe12333
parent8ad23a3b8e09a5eeea2b6bcd868356b5ce1e0112 (diff)
parentedf2f9d907a1b68ba6d19626f19ccee308e120a5 (diff)
downloadperlweeklychallenge-club-e52d437514981abcaec01d95542bcfc682125ea1.tar.gz
perlweeklychallenge-club-e52d437514981abcaec01d95542bcfc682125ea1.tar.bz2
perlweeklychallenge-club-e52d437514981abcaec01d95542bcfc682125ea1.zip
Merge pull request #1434 from noudald/challenge-052-noud
Solution for challenge 052 task 1 and 2 in Raku by Noud
-rw-r--r--challenge-052/noud/raku/ch-1.p640
-rw-r--r--challenge-052/noud/raku/ch-2.p646
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-052/noud/raku/ch-1.p6 b/challenge-052/noud/raku/ch-1.p6
new file mode 100644
index 0000000000..f6f3856b21
--- /dev/null
+++ b/challenge-052/noud/raku/ch-1.p6
@@ -0,0 +1,40 @@
+# Stepping Numbers
+#
+# Write a script to accept two numbers between 100 and 999. It should then
+# print all Stepping Numbers between them.
+#
+# A number is called a stepping number if the adjacent digits have a difference
+# of 1. For example, 456 is a stepping number but 129 is not.
+
+
+multi sub stepping-numbers($start, 0) {
+ [[$start],];
+}
+
+multi sub stepping-numbers($start, $size) {
+ gather {
+ if ($start != 0) {
+ for stepping-numbers($start - 1, $size - 1) -> @sn {
+ take [$start, |(@sn)];
+ }
+ }
+
+ if ($start != 9) {
+ for stepping-numbers($start + 1, $size - 1) -> @sn {
+ take [$start, |(@sn)];
+ }
+ }
+ }
+}
+
+sub stepping-number-range($start, $end) {
+ gather for $start.polymod(100)[1]..$end.polymod(100)[1] -> $i {
+ for stepping-numbers($i, 2) -> @sn {
+ my $sn = @sn.join('').Int;
+ take $sn if ($start <= $sn <= $end);
+ }
+ }
+}
+
+say stepping-number-range(101, 999);
+say stepping-number-range(545, 987);
diff --git a/challenge-052/noud/raku/ch-2.p6 b/challenge-052/noud/raku/ch-2.p6
new file mode 100644
index 0000000000..8471895e24
--- /dev/null
+++ b/challenge-052/noud/raku/ch-2.p6
@@ -0,0 +1,46 @@
+# Lucky Winner
+#
+# Suppose there are following coins arranged on a table in a line in random
+# order.
+#
+# £1, 50p, 1p, 10p, 5p, 20p, £2, 2p
+#
+# Suppose you are playing against the computer. Player can only pick one coin
+# at a time from either ends. Find out the lucky winner, who has the larger
+# amounts in total?
+
+use experimental :cached;
+
+# Compute the max amount of money a player can get when both players play
+# optimal. Note that this algorithm only computes the amount the player will
+# win if both the player and computer play optimal. This game is a min-max
+# game. Your optimize your gain by simultaneously minimizing the gain of your
+# opponent.
+sub max-amount(@coins) is cached {
+ if (@coins.elems < 2) {
+ return @coins.sum();
+ }
+
+ return @coins.sum() - min(max-amount(@coins[1..*-1]),
+ max-amount(@coins[0..*-2]));
+}
+
+
+# Convert coins on the table to cents
+my @coins = [100, 50, 1, 10, 5, 20, 200, 2];
+
+# Player wins
+my $player-win = max-amount(@coins);
+
+# Computer wins
+my $computer-win = @coins.sum() - $player-win;
+
+say 'Player wins: ', $player-win, ' cents.';
+say 'Computer wins: ', $computer-win, ' cents.';
+if ($player-win > $computer-win) {
+ say 'Player wins!';
+} elsif ($player-win < $computer-win) {
+ say 'Computer wins!';
+} else {
+ say 'Remise, neither player or computer wins.';
+}