diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-03-15 10:02:40 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-03-15 10:02:40 +0000 |
| commit | 9e86d8945acde5d75a3fbc69ad4f222a6d290a3b (patch) | |
| tree | 66741e547fc014c3b049f64dd08b6bd4de995e91 /challenge-104/james-smith | |
| parent | 65d54d52500028ec5359a7d39619803ade281543 (diff) | |
| download | perlweeklychallenge-club-9e86d8945acde5d75a3fbc69ad4f222a6d290a3b.tar.gz perlweeklychallenge-club-9e86d8945acde5d75a3fbc69ad4f222a6d290a3b.tar.bz2 perlweeklychallenge-club-9e86d8945acde5d75a3fbc69ad4f222a6d290a3b.zip | |
push ch104
Diffstat (limited to 'challenge-104/james-smith')
| -rw-r--r-- | challenge-104/james-smith/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-104/james-smith/perl/ch-2.pl | 41 |
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-104/james-smith/perl/ch-1.pl b/challenge-104/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..900a4a5919 --- /dev/null +++ b/challenge-104/james-smith/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say state); +use Test::More; + +my @known = (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); + +subtest 'fusc_seq' => sub { + is( join(' ',fusc_seq($_)), join ' ',@known[0..$_-1] ) foreach 0..@known-1; + done_testing(); +}; + +subtest 'fusc' => sub { + is( fusc($_), $known[$_-1] ) foreach 1..@known-1; + done_testing(); +}; + +done_testing(); +say join ' ',fusc_seq(50); + +sub fusc { + my $n = shift; + (fusc_seq($n))[$n-1]; +} + +sub fusc_seq { + my $n = shift; + return 0..$n-1 if $n<2; + my @seq = (0,1,1); ## We need to use one more seed value as the 5th entry + ## when we pushed it needs the 3rd one before it can + ## push as we are pushing two at a time... + + ## We can simplify the code by pushing two entries onto the list each time... + push @seq, $seq[$_]+$seq[$_+1], $seq[$_+1] foreach 1..$n/2-1; + + ## and if odd we just remove the last one we added... + pop @seq unless $n%2; + return @seq; +} diff --git a/challenge-104/james-smith/perl/ch-2.pl b/challenge-104/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..6dd73eaf7e --- /dev/null +++ b/challenge-104/james-smith/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +## This simulates a game with random selects.... +simulate() foreach 1..10; +## This simulates a game where player 1 takes a random number of token(s) +## Then player to takes the optimal number of tokens... to guarentee a win... +simulate_player2() foreach 1..10; + +sub simulate_player2 { + my $tokens = 12; + while(1) { + my $n = 1 + int rand 3; + $tokens -= $n; + say "Player 1 takes $n token(s) and leaves $tokens token(s)"; + $n = 4-$n; + print "Player 2 takes $n token(s) and "; + $tokens -= $n; + last unless $tokens; + say "leaves $tokens tokens"; + } + say "wins...\n"; +} + +sub simulate { + my($tokens,$player,$n) = (12,1,''); + while(1) { + $n = 1 + int rand 3; + last if $n >= $tokens; + $tokens-=$n; + say "Player $player takes $n token(s) and leaves $tokens token(s)"; + $player = 3-$player; + } + say "Player $player takes the last $n token(s) and wins...\n"; +} + |
