diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 19:31:37 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 19:31:37 +0800 |
| commit | f07721a447942936d7189b4beec3c2acae0c635d (patch) | |
| tree | 78297ed782b990844c47e2852a68cc336e3665ab | |
| parent | 5ed25077fde85262036c9db3e893d70ae0907b5c (diff) | |
| download | perlweeklychallenge-club-f07721a447942936d7189b4beec3c2acae0c635d.tar.gz perlweeklychallenge-club-f07721a447942936d7189b4beec3c2acae0c635d.tar.bz2 perlweeklychallenge-club-f07721a447942936d7189b4beec3c2acae0c635d.zip | |
challenge 104, raku solutions
| -rwxr-xr-x | challenge-104/feng-chang/raku/ch-1.raku | 12 | ||||
| -rwxr-xr-x | challenge-104/feng-chang/raku/ch-2.raku | 51 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-104/feng-chang/raku/ch-1.raku b/challenge-104/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..19d124c75a --- /dev/null +++ b/challenge-104/feng-chang/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/bin/env raku + +multi fusc(0) { 0 } +multi fusc(1) { 1 } + +multi fusc(UInt:D \n where n > 0 && n %% 2) +{ fusc((n/2).UInt) } + +multi fusc(UInt:D \n where * > 0) +{ fusc(((n-1)/2).UInt) + fusc(((n+1)/2).UInt) } + +put (0..49)».&fusc.join(' '); diff --git a/challenge-104/feng-chang/raku/ch-2.raku b/challenge-104/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..81d4200622 --- /dev/null +++ b/challenge-104/feng-chang/raku/ch-2.raku @@ -0,0 +1,51 @@ +#!/bin/env raku + +sub human-play(UInt:D \n --> UInt:D) { + my UInt $pick; + + repeat { + $pick = prompt "{ n } tokens left, please input number of tokens you take: "; + } while $pick != 1|2|3 || $pick > n; + put ''; + + $pick; +} + +sub computer-play(UInt:D \n --> UInt:D) { + my UInt $pick = do given n { + when n < 4 { n } + when n %% 4 { (1..3).pick } + default { n % 4 } + }; + put "{ n } tokens left, I take $pick tokens.\n"; + + $pick; +} + +multi MAIN(UInt:D $n is copy, Bool:D \human-plays-first = True) { + my Bool $human-won = True; + + while $n > 0 { + given human-plays-first { + when ?* { + $n -= human-play($n); + + if $n > 0 { + $n -= computer-play($n); + $human-won = False if $n == 0; + } + } + default { + $n -= computer-play($n); + + if $n == 0 { + $human-won = False; + } else { + $n -= human-play($n); + } + } + } + } + + put "{ $human-won ?? 'You' !! 'I' } won."; +} |
