diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2020-03-16 18:38:02 +0100 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2020-03-16 18:38:02 +0100 |
| commit | 3d6993c716e77f68ca653de277640d6cdd4790ca (patch) | |
| tree | 4d71e54aa720f6d8608099dbacef90c967303cae | |
| parent | b3472c66c6546f3373094f47c7889fa33cbc7b1b (diff) | |
| download | perlweeklychallenge-club-3d6993c716e77f68ca653de277640d6cdd4790ca.tar.gz perlweeklychallenge-club-3d6993c716e77f68ca653de277640d6cdd4790ca.tar.bz2 perlweeklychallenge-club-3d6993c716e77f68ca653de277640d6cdd4790ca.zip | |
Possible task 2 implementation.
| -rw-r--r-- | challenge-052/luca-ferrari/raku/ch-2.p6 | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-052/luca-ferrari/raku/ch-2.p6 b/challenge-052/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..edbc65125b --- /dev/null +++ b/challenge-052/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,59 @@ +#!env raku +# +# +# Task 2 +# <https://perlweeklychallenge.org/blog/perl-weekly-challenge-052/> +# +# 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? + +my @moneys = 1, 0.5, 0.01, 0.05, 0.2, 2, 0.02; + + +@moneys.say; +my @moves; + +my ( @player, @computer ); + + +# First approach: let's cheat and make the player choose always the max +# value there is on the table +while ( @moneys.elems ) { + my $left = @moneys.shift || 0; + my $right = @moneys.pop || 0; + + @player.push: $left > $right ?? $left !! $right; + @computer.push: $left > $right ?? $right !! $left; +} + + +say "Player wins with { @player } = { [+] @player } vs { @computer } = { [+] @computer }"; + + +say "Let's play another random turn"; + +@player = (); +@computer = (); +@moneys = 1, 0.5, 0.01, 0.05, 0.2, 2, 0.02; + +while ( @moneys.elems ) { + if 99.rand.Int %% 2 { + @player.push: @moneys.shift || 0; + @computer.push: @moneys.pop || 0; + } + else { + @player.push: @moneys.pop || 0; + @computer.push: @moneys.shift || 0; + + } +} + +my $player-score = [+] @player; +my $computer-score = [+] @computer; + +say "Computer wins with { @computer } = $computer-score VS { @player } = $player-score" if $computer-score > $player-score; +say "Computer looses with { @computer } = $computer-score VS { @player } = $player-score" if $computer-score < $player-score; +say "Tie!" if $computer-score == $player-score; |
