aboutsummaryrefslogtreecommitdiff
path: root/challenge-104
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-21 08:02:05 +0100
committerGitHub <noreply@github.com>2021-03-21 08:02:05 +0100
commit3eeb017628ca0a7bdb9a5cddf23cf80e79a29c07 (patch)
treef43ac6dcca8d8e55a0da65731c75f2300259478f /challenge-104
parent5e7e7db1dd5845a917ad63c0ab9ab38fea5a12a7 (diff)
parent2eeba984975199f3be6a58415d19e1350d868636 (diff)
downloadperlweeklychallenge-club-3eeb017628ca0a7bdb9a5cddf23cf80e79a29c07.tar.gz
perlweeklychallenge-club-3eeb017628ca0a7bdb9a5cddf23cf80e79a29c07.tar.bz2
perlweeklychallenge-club-3eeb017628ca0a7bdb9a5cddf23cf80e79a29c07.zip
Merge pull request #3745 from wambash/challenge-week-104
Challenge week 104
Diffstat (limited to 'challenge-104')
-rw-r--r--challenge-104/wambash/raku/ch-1.raku33
-rw-r--r--challenge-104/wambash/raku/ch-2.raku56
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-104/wambash/raku/ch-1.raku b/challenge-104/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..951be974b7
--- /dev/null
+++ b/challenge-104/wambash/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+my @fusc = (^∞).map: *.&fusc ;
+multi fusc( 0 --> 0 ) {}
+multi fusc( 1 --> 1 ) {}
+multi fusc(Int $n where $n %% 2) { @fusc[$n/2] };
+multi fusc(Int $n) { @fusc[($n-1)/2] + @fusc[($n+1)/2] };
+
+multi MAIN ($n) {
+ say @fusc[$n]
+}
+
+multi MAIN (Bool :$test!) {
+ use Test;
+
+ my @fusc-test = (
+ 0, 1, 1, 2, 1, 3, 2, 3,
+ 1, 4, 3, 5, 2, 5, 3, 4,
+ 1, 5, 4, 7, 3, 8, 5, 7,
+ 2, 7, 5, 8, 3, 7, 4, 5,
+ 1, 6, 5, 9, 4, 11, 7, 10,
+ 3, 11, 8, 13, 5, 12, 7, 9,
+ 2, 9, 7, 12, 5, 13, 8, 11, 3,
+ 10, 7, 11, 4, 9, 5, 6, 1, 7, 6,
+ 11, 5, 14, 9, 13, 4, 15, 11, 18, 7,
+ 17, 10, 13, 3, 14, 11, 19, 8, 21,
+ 13, 18, 5, 17, 12, 19,
+ );
+
+ is @fusc.head(92), @fusc-test;
+
+ done-testing;
+}
diff --git a/challenge-104/wambash/raku/ch-2.raku b/challenge-104/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..d7c6eab80a
--- /dev/null
+++ b/challenge-104/wambash/raku/ch-2.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/env raku
+
+sub random ( $tokens ) {
+ (1..3).pick min $tokens
+}
+
+sub smart ( $tokens ) {
+ ($tokens % 4) max 1
+}
+
+sub semi-smart ( $tokens ) {
+ (&smart,&random).pick.($tokens)
+}
+
+sub human ( $tokens ) {
+ prompt('pick 1, 2 or 3: ')
+}
+
+my %players = :&human, :&smart, :&random, :&semi-smart;
+
+subset Pick of Int where * ∈ 1..3;
+sub play ($tokens, &rival) {
+ my Pick $pick = $tokens.&rival;
+ $tokens - $pick;
+}
+
+sub nim ( :&fp, :&sp, :$tokens = 12 ) {
+ $tokens, |(|(&fp, &sp) xx *)
+ andthen $_ Z=> .produce: &play
+ andthen .first: *.value ≤ 0
+ andthen .key.name;
+}
+
+
+multi MAIN (
+ $first-player = 'human', #= human. smart, semi-smart or random
+ $second-player = 'semi-smart', #= human. smart, semi-smart or random
+ Int :$tokens = 12,
+) {
+ &play.wrap: -> $tokens, &rival {
+ say "$tokens tokens {&rival.name} plays";
+ callsame;
+ }
+ say nim( fp => %players{$first-player}, sp => %players{$second-player}, :$tokens ), ' wins'
+}
+
+
+multi MAIN (Bool :$test! ) {
+ use Test;
+ is smart(4), 1;
+ is smart(17),1;
+ is play(12,&smart), 11;
+ is play(23,&smart), 20;
+ is nim(fp => &smart, sp => &random, tokens => 21), 'smart', 'smart wins with 21 tokens';
+ done-testing;
+}