From d7b0ff91b6c9d1ac34a4b23b46e565dc16e6c1f8 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 15 Mar 2021 09:00:38 +0100 Subject: Task 1 done --- challenge-104/luca-ferrari/raku/ch-1.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 challenge-104/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-104/luca-ferrari/raku/ch-1.p6 b/challenge-104/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..723ca7793a --- /dev/null +++ b/challenge-104/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!raku + +# fusc(0) = 0 +# fusc(1) = 1 +# for n > 1: +# when n is even: fusc(n) = fusc(n / 2), +# when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2) + + +multi sub fusc( 0 ) { 0 } +multi sub fusc( 1 ) { 1 } +multi sub fusc( $n where { $n > 1 } ) { + return samewith( ( $n / 2 ).Int ) if $n %% 2; + return samewith( ( ( $n - 1 ) / 2 ).Int ) + samewith( ( ( $n + 1 ) / 2 ).Int ); +} + + +sub MAIN(){ + "fusc( $_ ) = { fusc( $_ )}".say for 0 .. 10; +} -- cgit From e94c3e189426bdf3b67adf7ea903134efdd545e5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 15 Mar 2021 09:13:05 +0100 Subject: Task 2 done --- challenge-104/luca-ferrari/raku/ch-2.p6 | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-104/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-104/luca-ferrari/raku/ch-2.p6 b/challenge-104/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..660c217643 --- /dev/null +++ b/challenge-104/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#!raku + +# a) You have 12 tokens +# b) Each player can pick 1, 2 or 3 tokens at a time +# c) The player who picks the last token wins the game + + +sub MAIN() { + # all tokens are True-selectable + my @tokens = True xx 12; + + my @players = ; + + while ( @tokens.elems > 0 ) { + for @players -> $player { + my $how-many = ( 1 .. min( 3, @tokens.elems ) ).pick; + @tokens.pop for 1 .. $how-many; + say "$player picks $how-many, remaining { @tokens.elems }"; + + if @tokens.elems == 0 && $how-many > 0 { + "Player $player won!".say && exit; + } + } + + } + +} -- cgit From d49b98c6e078c0aa5c59e952efc8c8c772d79552 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 15 Mar 2021 09:27:44 +0100 Subject: Blog references --- challenge-104/luca-ferrari/blog-1.txt | 1 + challenge-104/luca-ferrari/blog-2.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 challenge-104/luca-ferrari/blog-1.txt create mode 100644 challenge-104/luca-ferrari/blog-2.txt diff --git a/challenge-104/luca-ferrari/blog-1.txt b/challenge-104/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ab103a6810 --- /dev/null +++ b/challenge-104/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/03/15/PerlWeeklyChallenge104.html#task1 diff --git a/challenge-104/luca-ferrari/blog-2.txt b/challenge-104/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..2dfda9c979 --- /dev/null +++ b/challenge-104/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/03/15/PerlWeeklyChallenge104.html#task2 -- cgit From 18b5f3975cfa1a564f5953dac6ef844576742d0d Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 15 Mar 2021 09:06:00 +0000 Subject: FUSC sequence --- challenge-104/simon-proctor/raku/ch-1.raku | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-104/simon-proctor/raku/ch-1.raku diff --git a/challenge-104/simon-proctor/raku/ch-1.raku b/challenge-104/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..b4bedc3cc1 --- /dev/null +++ b/challenge-104/simon-proctor/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + + +subset Even of Any where { $_ %% 2 }; +subset Odd of Any where { $_ !%% 2 }; + +multi sub fusc($ where * == 0) { 0 } +multi sub fusc($ where * == 1) { 1 } +multi sub fusc(Even \n) { fusc( n / 2 ) } +multi sub fusc(Odd \n) { fusc( (n-1)/2 ) + fusc( (n+1)/2 ) } + +#| Generate the first n FUSC numbers +sub MAIN( UInt \n ) { + (0..^n).map( { fusc($_) } ).join(", ").say; +} -- cgit From dcf2605ec4f2ce6466316bdd06f7023eb2d9a2b9 Mon Sep 17 00:00:00 2001 From: Alexander Karelas Date: Mon, 15 Mar 2021 11:14:43 +0200 Subject: solutions --- challenge-104/alexander-karelas/perl/ch-1.pl | 19 ++++++++++ challenge-104/alexander-karelas/perl/ch-2.pl | 52 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 challenge-104/alexander-karelas/perl/ch-1.pl create mode 100755 challenge-104/alexander-karelas/perl/ch-2.pl diff --git a/challenge-104/alexander-karelas/perl/ch-1.pl b/challenge-104/alexander-karelas/perl/ch-1.pl new file mode 100755 index 0000000000..14b2188772 --- /dev/null +++ b/challenge-104/alexander-karelas/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use v5.30; +use warnings; + +use experimental 'signatures'; + +sub fusc ($n) { + return $n if $n <= 1; + if ($n % 2 == 0) { + return fusc($n / 2); + } else { + return fusc(($n - 1) / 2) + fusc(($n + 1) / 2); + } +} + +for (my $i = 0; $i < 50; $i++) { + say $i, ": ", fusc($i); +} \ No newline at end of file diff --git a/challenge-104/alexander-karelas/perl/ch-2.pl b/challenge-104/alexander-karelas/perl/ch-2.pl new file mode 100755 index 0000000000..933a4ff87a --- /dev/null +++ b/challenge-104/alexander-karelas/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +use v5.30; +use warnings; + +use experimental 'signatures'; + +use List::Util 'max', 'min'; + +sub position_value ($n) { + if ($n == 0) { + return -1; + } + + my @move_values; + for my $move (1 .. min(3, $n)) { + $move_values[$move] = - position_value($n - $move); + } + + return max(grep defined, @move_values); +} + +sub best_move ($n) { + my $pos_value = position_value($n); + my @best_moves = grep position_value($n - $_) == - $pos_value, (1 .. min(3, $n)); + return $best_moves[int rand @best_moves]; +} + +my $num_sticks = 12; + +print "Who plays first? You or computer? (y/c): "; +chomp(my $player = ); + +while ($num_sticks > 0) { + say "$num_sticks sticks remain."; + my $num_pick; + if ($player eq 'c') { + $num_pick = best_move($num_sticks); + say "I pick $num_pick."; + } else { + print "How many do you pick? "; + chomp($num_pick = ); + } + $num_sticks -= $num_pick; + $player = $player eq 'c' ? 'y' : 'c'; +} + +if ($player eq 'y') { + say "Computer wins!"; +} else { + say "You win"; +} -- cgit From 9e86d8945acde5d75a3fbc69ad4f222a6d290a3b Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 15 Mar 2021 10:02:40 +0000 Subject: push ch104 --- challenge-104/james-smith/perl/ch-1.pl | 42 ++++++++++++++++++++++++++++++++++ challenge-104/james-smith/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 challenge-104/james-smith/perl/ch-1.pl create mode 100644 challenge-104/james-smith/perl/ch-2.pl 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"; +} + -- cgit From ea81c1e0ae61c63821b9411d9ec53bdb82d5addc Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 15 Mar 2021 11:34:24 +0000 Subject: added caching version of the seq function to speed up multiple calls --- challenge-104/james-smith/perl/ch-1.pl | 38 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/challenge-104/james-smith/perl/ch-1.pl b/challenge-104/james-smith/perl/ch-1.pl index 900a4a5919..1edbf506e4 100644 --- a/challenge-104/james-smith/perl/ch-1.pl +++ b/challenge-104/james-smith/perl/ch-1.pl @@ -6,19 +6,32 @@ 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); +my @K = (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); +## Test both subroutines.. subtest 'fusc_seq' => sub { - is( join(' ',fusc_seq($_)), join ' ',@known[0..$_-1] ) foreach 0..@known-1; + is( join(' ',fusc_seq($_)), join ' ',@K[0..$_-1] ) foreach 0..@K-1; + done_testing(); +}; + +subtest 'fusc_seq_cache' => sub { + is( join(' ',fusc_seq_cache($_)), join ' ',@K[0..$_-1] ) foreach reverse 0..@K-1; done_testing(); }; subtest 'fusc' => sub { - is( fusc($_), $known[$_-1] ) foreach 1..@known-1; + is( fusc($_), $K[$_-1] ) foreach 1..@K-1; done_testing(); }; done_testing(); + +## Print the requested string.... say join ' ',fusc_seq(50); sub fusc { @@ -29,14 +42,25 @@ sub fusc { 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... + my @seq = (0,1,1); ## We need to use one more seed value as the 4th + ## entry when we push it needs the 3rd one already + ## defined as we are pushing two at a time... - ## We can simplify the code by pushing two entries onto the list each 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; } + +## Added ac caching version - this time we remember the list using a +## state variable - and use array slice operator to return the part +## required (if cache bigger than requested chunk) +sub fusc_seq_cache { + my $n = shift; + state @seq = (0,1,1); + push @seq, $seq[$_]+$seq[$_+1], $seq[$_+1] foreach @seq/2..$n/2-1; + return @seq[0..$n-1]; +} -- cgit From 75df0fc1c419f52f82a385fdd2d81e78836a5a43 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 15 Mar 2021 11:41:08 +0000 Subject: display list from each method! --- challenge-104/james-smith/perl/ch-1.pl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/challenge-104/james-smith/perl/ch-1.pl b/challenge-104/james-smith/perl/ch-1.pl index 1edbf506e4..9d8d2eb4cd 100644 --- a/challenge-104/james-smith/perl/ch-1.pl +++ b/challenge-104/james-smith/perl/ch-1.pl @@ -32,11 +32,13 @@ subtest 'fusc' => sub { done_testing(); ## Print the requested string.... -say join ' ',fusc_seq(50); +say join ' ',map { fusc($_) } 1..50; +say join ' ',map { $_ } fusc_seq(50); +say join ' ',fusc_seq_cache(50); sub fusc { my $n = shift; - (fusc_seq($n))[$n-1]; + return $n ? (fusc_seq($n))[$n-1] : ''; } sub fusc_seq { -- cgit From dbb8361d66d9db35bcc45b88d77792b5f034c187 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 15 Mar 2021 13:14:46 +0100 Subject: README for week 104 --- challenge-104/abigail/README.md | 140 ++++++++-------------------------------- 1 file changed, 27 insertions(+), 113 deletions(-) diff --git a/challenge-104/abigail/README.md b/challenge-104/abigail/README.md index f8b761268c..3e6ba1fe6b 100644 --- a/challenge-104/abigail/README.md +++ b/challenge-104/abigail/README.md @@ -1,137 +1,51 @@ # Solution by Abigail -## [Chinese Zodiac](https://perlweeklychallenge.org/blog/perl-weekly-challenge-103/#TASK1) +## [FUSC Sequence](https://perlweeklychallenge.org/blog/perl-weekly-challenge-104/#TASK1) -You are given a year `$year`. +Write a script to generate first 50 members of FUSC Sequence. Please +refer to [OEIS](https://oeis.org/A002487) for more information._ -Write a script to determine the *Chinese Zodiac* for the given year -$year. Please check out [wikipage](https://en.wikipedia.org/wiki/Chinese_zodiac) -for more information about it. +The sequence defined as below: -The animal cycle: -* Rat -* Ox -* Tiger -* Rabbit -* Dragon -* Snake -* Horse -* Goat -* Monkey -* Rooster -* Dog -* Pig - -The element cycle: -* Wood -* Fire -* Earth -* Metal -* Water - -### Examples -#### Example 1 -~~~~ -Input: 2017 -Output: Fire Rooster -~~~~ - -#### Example 2 ~~~~ -Input: 1938 -Output: Earth Tiger +fusc(0) = 0 +fusc(1) = 1 +for n > 1: +when n is even: fusc(n) = fusc(n / 2), +when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2) ~~~~ ### Notes -We will actually output a three part string; beside the animal and element -cycles, we also output the yin/yang cycle. - -We will be reading years from standard input, writing results to standard -output. +This challenge is beyond simple. Since we're asked to generate a +fixed number of numbers, we don't need do any calculations, or +even handle a single if statement. A single print statement is enough. ### Solutions -* [AWK](awk/ch-1.awk) -* [Bash](bash/ch-1.sh) -* [Befunge-93](befunge-93/ch-1.bf93) -* [C](c/ch-1.c) -* [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) -* [Perl](perl/ch-1.pl) -* [Python](python/ch-1.py) -* [Ruby](ruby/ch-1.rb) ### Blog -[Perl Weekly Challenge 103: Chinese Zodiac](https://wp.me/pcxd30-uS) - -## [What's playing?](https://perlweeklychallenge.org/blog/perl-weekly-challenge-103/#TASK2) -Working from home, you decided that on occasion you wanted some -background noise while working. You threw together a network streamer -to continuously loop through the files and launched it in a tmux -(or screen) session, giving it a directory tree of files to play. -During the day, you connected an audio player to the stream, listening -through the workday, closing it when done. -For weeks you connect to the stream daily, slowly noticing a gradual -drift of the media. After several weeks, you take vacation. When -you return, you are pleasantly surprised to find the streamer still -running. Before connecting, however, if you consider the puzzle of -determining which track is playing. +## [NIM Game](https://perlweeklychallenge.org/blog/perl-weekly-challenge-104/#TASK2) +Write a script to simulate the NIM Game. -After looking at a few modules to read info regarding the media, a -quick bit of coding gave you a file list. The file list is in a -simple CSV format, each line containing two fields: the first the -number of milliseconds in length, the latter the media's title (this -example is of several episodes available from the MercuryTheatre.info): +It is played between 2 players. For the purpose of this task, let +assume you play against the machine. +There are 3 simple rules to follow: ~~~~ -1709363,"Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)" -1723781,"Les Miserables Episode 2: Javert (broadcast date: 1937-07-30)" -1723781,"Les Miserables Episode 3: The Trial (broadcast date: 1937-08-06)" -1678356,"Les Miserables Episode 4: Cosette (broadcast date: 1937-08-13)" -1646043,"Les Miserables Episode 5: The Grave (broadcast date: 1937-08-20)" -1714640,"Les Miserables Episode 6: The Barricade (broadcast date: 1937-08-27)" -1714640,"Les Miserables Episode 7: Conclusion (broadcast date: 1937-09-03)" +a) You have 12 tokens +b) Each player can pick 1, 2 or 3 tokens at a time +c) The player who picks the last token wins the game ~~~~ -For this script, you can assume to be provided the following information: -* the value of `$^T` (`$BASETIME`) of the streamer script, -* the value of `time()`, and -* a CSV file containing the media to play consisting of the length in - milliseconds and an identifier for the media (title, filename, or other). - -Write a program to output which file is currently playing. For -purposes of this script, you may assume gapless playback, and format -the output as you see fit. - -Optional: Also display the current position in the media as a time-like value. - -### Example -~~~~ -Input: 3 command line parameters: start time, current time, file name - - # Streamer start time: Tue Nov 24 12:22:03 2020 - 1606134123 - - # Current time: Mon Mar 1 09:34:36 2021 - 1614591276 - - # filelist.csv - -Output: +### Notes +This is *not* the NIM game. In the NIM game, you have multiple sets +of tokens, and on each turn, a player picks a set, and removes any +non-zero number of of tokens from the set. Typically, the player +picking the last token loses. - "Les Miserables Episode 1: The Bishop (broadcast date: 1937-07-23)" - 00:17:33 -~~~~ +The game as given is a win for the second player, who will win on +the third move. ### Solutions -* [GNU AWK](awk/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [C](c/ch-2.c) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) -* [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 103: What's playing?](https://wp.me/pcxd30-v6) -- cgit From e5112534f5dddae221cb17e4df99ab51800949bb Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 15 Mar 2021 08:21:10 -0400 Subject: 1st commit on 104_raku --- challenge-104/stuart-little/raku/ch-1.p6 | 14 ++++++++++++++ challenge-104/stuart-little/raku/ch-2.p6 | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 challenge-104/stuart-little/raku/ch-1.p6 create mode 100755 challenge-104/stuart-little/raku/ch-2.p6 diff --git a/challenge-104/stuart-little/raku/ch-1.p6 b/challenge-104/stuart-little/raku/ch-1.p6 new file mode 100755 index 0000000000..3a09403f0b --- /dev/null +++ b/challenge-104/stuart-little/raku/ch-1.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/env perl6 +use v6; + +# run