diff options
| author | mimosinnet <mimosinnet@gmail.com> | 2021-03-21 21:59:59 +0100 |
|---|---|---|
| committer | mimosinnet <mimosinnet@gmail.com> | 2021-03-21 21:59:59 +0100 |
| commit | 0a3d0ebbb64f39006e5252538d058cb72dcb10cf (patch) | |
| tree | d2a82476352d784040dacfe5c61e79af7ef447c0 /challenge-104 | |
| parent | daef95a1472a2433419bb2adedd7e74bee851261 (diff) | |
| download | perlweeklychallenge-club-0a3d0ebbb64f39006e5252538d058cb72dcb10cf.tar.gz perlweeklychallenge-club-0a3d0ebbb64f39006e5252538d058cb72dcb10cf.tar.bz2 perlweeklychallenge-club-0a3d0ebbb64f39006e5252538d058cb72dcb10cf.zip | |
Solution for challenge 104
Diffstat (limited to 'challenge-104')
| -rw-r--r-- | challenge-104/mimosinnet/raku/ch-1.raku | 24 | ||||
| -rw-r--r-- | challenge-104/mimosinnet/raku/ch-2.raku | 65 |
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-104/mimosinnet/raku/ch-1.raku b/challenge-104/mimosinnet/raku/ch-1.raku new file mode 100644 index 0000000000..7d8f87533c --- /dev/null +++ b/challenge-104/mimosinnet/raku/ch-1.raku @@ -0,0 +1,24 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-104/ + +multi sub challenge( $n ) { + return 0 if $n == 0; + return 1 if $n == 1; + return challenge( $n /2) if $n %% 2; + return challenge( ($n-1) / 2 ) + challenge( ($n+1) / 2 ); +} + +multi sub MAIN( $n ) { + ( 0..($n-1 ) ).map({ challenge($_) }).say; +} + +multi sub MAIN( 'challenge' ) { + MAIN( 50 ); +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @fusc = <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>; + + ( 0..(@fusc.elems -1) ).map({ is challenge($_),@fusc[$_],@fusc[$_] }); +} diff --git a/challenge-104/mimosinnet/raku/ch-2.raku b/challenge-104/mimosinnet/raku/ch-2.raku new file mode 100644 index 0000000000..e656553733 --- /dev/null +++ b/challenge-104/mimosinnet/raku/ch-2.raku @@ -0,0 +1,65 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-104/ + +class NIM { + has $!tokens = 12; + has $.name; + has $.turn is rw; + + method token-human-get() { + my $token; + loop { + $token = prompt "$!name, there are $!tokens tokens left. How many tokens would you like to pick? (1-3) "; + last if $token ∈ (1..3); + say 'You have tu choose between 1 and 3 tokens'; + } + return $token; + } + + # The computer strategy follows athanasius solution + method token-comp-get() { + return $!tokens if $!tokens < 4; + return $!tokens % 4 unless $!tokens %% 4; + return (1..3).pick; + } + + method start-game() { + loop { + given $!turn { + when 'human' { + $!tokens = $!tokens - self.token-human-get(); + self.win if $!tokens == 0; + $!turn = 'comp'; + } + when 'comp' { + my $get-tokens = self.token-comp-get(); + say "There are $!tokens tokens left. I get $get-tokens"; + $!tokens = $!tokens - $get-tokens; + self.win if $!tokens == 0; + $!turn = 'human'; + } + } + } + } + + method win() { + given $!turn { + when 'human' { say "Congratualtions $.name. You have won" } + when 'comp' { say "I am sorry $.name. I have won" } + } + exit; + } +} + +sub MAIN( $name = 'Human' ) { + my $nim = NIM.new( name => $name ); + say "Hello $name"; + if Bool.pick { + say 'I start the game'; + $nim.turn = 'comp'; + } else { + say 'You start the game'; + $nim.turn = 'human'; + } + $nim.start-game; +} + |
