diff options
| -rw-r--r-- | challenge-053/ryan-thompson/README.md | 57 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/raku/ch-1.p6 | 39 | ||||
| -rw-r--r-- | challenge-053/ryan-thompson/raku/ch-2.p6 | 25 |
7 files changed, 162 insertions, 52 deletions
diff --git a/challenge-053/ryan-thompson/README.md b/challenge-053/ryan-thompson/README.md index e61bf17dfd..f0cab5ade3 100644 --- a/challenge-053/ryan-thompson/README.md +++ b/challenge-053/ryan-thompson/README.md @@ -1,66 +1,19 @@ # Ryan Thompson -## Week 052 Solutions +## Week 053 Solutions -### Task 1 › Stepping Numbers +### Task 1 › Rotate Matrix * [Perl](perl/ch-1.pl) * [Raku](raku/ch-1.p6) -### Task 2 › Lucky Winner +### Task 2 › Vowel Strings * [Perl](perl/ch-2.pl) * [Raku](raku/ch-2.p6) ## Blogs - * [Week 052 › Stepping Numbers](http://ry.ca/2020/03/stepping-numbers/) - * [Week 052 › Lucky Winner](http://ry.ca/2020/03/lucky-winner/) + * [Week 052 › Rotate Matrix](http://ry.ca/2020/03/matrix-rotation/) + * [Week 052 › Vowel Strings](http://ry.ca/2020/03/vowel-strings/) -*** - -## Documentation for `perl/ch-2.pl` - - -# NAME - -ch-2.pl - Lucky Winner Simulator 9000 - -# SYNOPSIS - -``` -ch-2.pl [options] [algorithm1 algorithm2 ...] -ch-2.pl --human=<cpu_algorithm> -ch-2.pl --help -``` - -# OPTIONS - -``` ---count=<iter> Play <iter> games Default: 1000 ---coins=<N> Every game uses <N> coins Default: 8 ---maxcoin=<N> Maximum coin value Default: 200 ---help Full help page ---human=<cpu_alg> Human vs CPU, CPU uses <cpu_alg> ---seed=<N> Use specific random number seed (integer) ---verbose Enable extra output ---noverbose Disable extra output -``` - -# ALGORITHMS - -- `human` › Human input. Only available with `--human` option. - -- `bozo` › Real stupid algorithm; chooses left or right randomly. - -- `worst` › Somehow even stupider. Always picks lowest option. - -- `greedy` › Greedy algorithm. Always picks highest option, but doesn't look - ahead. - -- `ahead1`, `ahead3`, `ahead5` › Looks ahead **1**, **3**, or **5** turns, and - picks the option that maximizes (**my\_score** - **their\_score**) - -# AUTHOR - -Ryan Thompson <rjt@cpan.org> diff --git a/challenge-053/ryan-thompson/blog.txt b/challenge-053/ryan-thompson/blog.txt new file mode 100644 index 0000000000..dbb3e30892 --- /dev/null +++ b/challenge-053/ryan-thompson/blog.txt @@ -0,0 +1 @@ +http://www.ry.ca/2020/03/matrix-rotation/ diff --git a/challenge-053/ryan-thompson/blog1.txt b/challenge-053/ryan-thompson/blog1.txt new file mode 100644 index 0000000000..a7c53d6238 --- /dev/null +++ b/challenge-053/ryan-thompson/blog1.txt @@ -0,0 +1 @@ +http://www.ry.ca/2020/03/vowel-strings/ diff --git a/challenge-053/ryan-thompson/perl/ch-1.pl b/challenge-053/ryan-thompson/perl/ch-1.pl new file mode 100644 index 0000000000..b1656084d8 --- /dev/null +++ b/challenge-053/ryan-thompson/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# +# ch-1.pl - Rotate matrix +# +# 2020 Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; +no warnings 'uninitialized'; + +# Rotate a matrix clockwise +sub rotate90_cw { + my @A = reverse @{$_[0]}; + my @T; + for my $x (0..$#A) { + $T[$_][$x] = $A[$x][$_] for 0..$#{$A[0]}; + } + \@T; +} + +# Convenience +sub rotate180 { rotate90_cw( rotate90_cw (@_) ) } +sub rotate_ccw { rotate90_cw( rotate180( @_) ) } + +# +# Tests +# +use Test::More; + +# 3x3 +is_deeply rotate90_cw ( [[1,2,3],[4,5,6],[7,8,9]]), [[7,4,1],[8,5,2],[9,6,3]]; +is_deeply rotate180( [[1,2,3],[4,5,6],[7,8,9]]), [[9,8,7],[6,5,4],[3,2,1]]; +is_deeply rotate_ccw( [[1,2,3],[4,5,6],[7,8,9]]), [[3,6,9],[2,5,8],[1,4,7]]; + +# MxN +is_deeply rotate90_cw ( [[1,2],[3,4],[5,6]]), [[5,3,1],[6,4,2]]; +is_deeply rotate180( [[1,2],[3,4],[5,6]]), [[6,5],[4,3],[2,1]]; + +# 1xN, Nx1 +is_deeply rotate90_cw( [[1,2,3]]), [[1],[2],[3]]; +is_deeply rotate90_cw( [[1],[2],[3]]), [[3,2,1]]; + +# Non-numeric, because why not +is_deeply rotate90_cw ( [['a','b','c'], ['d','e','f']] ), + [['d','a'],['e','b'],['f','c']]; + +done_testing; diff --git a/challenge-053/ryan-thompson/perl/ch-2.pl b/challenge-053/ryan-thompson/perl/ch-2.pl new file mode 100644 index 0000000000..081f3895db --- /dev/null +++ b/challenge-053/ryan-thompson/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +# +# ch-2.pl - Vowel strings +# +# Ryan Thompson <rjt@cpan.org> + +use 5.010; +use warnings; +use strict; + +# Follow rules. Basically graph edges. +my %edges = ( a => ['i','e'], e => ['i'], i => ['u','o','e','a'], + o => ['u','a'], u => ['e','o'] ); + +# Builds vowel strings using BFS with queue +sub vowel_string { + my ($len) = @_; + my @queue = sort keys %edges; # Pre-load queue + + my @vstrs; + while (my $str = shift @queue) { + push @vstrs, $str and edges if $len <= length $str; + push @queue, $str.$_ for @{$edges{ substr $str, -1 }} + } + @vstrs; +} + +# Iterator version +sub vowel_iter { + my ($len) = @_; + my @queue = sort keys %edges; + sub { + while (my $str = shift @queue) { + return $str if $len <= length $str; + push @queue, $str.$_ for @{$edges{ substr $str, -1 }} + } + } +} + +say for vowel_string(6); + +my $it = vowel_iter(3); +say while $_ = $it->(); diff --git a/challenge-053/ryan-thompson/raku/ch-1.p6 b/challenge-053/ryan-thompson/raku/ch-1.p6 new file mode 100644 index 0000000000..746c7f032d --- /dev/null +++ b/challenge-053/ryan-thompson/raku/ch-1.p6 @@ -0,0 +1,39 @@ +#!/usr/bin/env perl6 + +# skeleton.p6 - Rotate Matrix +# +# Ryan Thompson <rjt@cpan.org> + +sub transpose(@A) { [Z] @A } + +multi sub rotate-cw(@A) { [Z] @A.reverse } +multi sub rotate-cw(@A where !*[0].isa("List")) { @A.map: { [$_] } } + +sub rotate180(@A) { rotate-cw( rotate-cw(@A) ) } +sub rotate-ccw(@A) { rotate180( rotate-cw(@A) ) } + + +# +# Tests +# + +use Test; + +# Matrix transposition +is-deeply transpose((1,2,3; 4,5,6; 7,8,9)), (1,4,7; 2,5,8; 3,6,9); + +# Rotation (mirrored transpose) +is-deeply rotate-cw( (1,2,3; 4,5,6; 7,8,9)), (7,4,1; 8,5,2; 9,6,3); +is-deeply rotate180((1,2,3; 4,5,6; 7,8,9)), (9,8,7; 6,5,4; 3,2,1); +is-deeply rotate-ccw((1,2,3; 4,5,6; 7,8,9)), (3,6,9; 2,5,8; 1,4,7); + +# MxN +is-deeply rotate-cw((1,2; 3,4; 5,6)), (5,3,1; 6,4,2); +is-deeply rotate180((1,2; 3,4; 5,6)), (6,5; 4,3; 2,1); + +# 1xN, Nx1 +is-deeply rotate-cw((1,2,3)), ([1],[2],[3]); +is-deeply rotate-cw([ [1],[2],[3] ]), ((3,2,1),); + +# Non-numeric, because why not +is-deeply rotate-cw(('a','b','c'; 'd','e','f')), ('d','a'; 'e','b'; 'f','c'); diff --git a/challenge-053/ryan-thompson/raku/ch-2.p6 b/challenge-053/ryan-thompson/raku/ch-2.p6 new file mode 100644 index 0000000000..55143f917d --- /dev/null +++ b/challenge-053/ryan-thompson/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env perl6 + +# ch-2.p6 - Vowel Strings +# +# Ryan Thompson <rjt@cpan.org> + +#| Graph edges. Key = current node, values => reachable nodes +my %edges = :a<e i>, :e<i>, :i<a e o u>, :o<a u>, :u<o e>; + +#| Builds vowel strings using BFS with queue +sub vowel-string-iter( Int $len where $len > 0 ) { + my @queue = %edges.keys.sort; + sub { + while (my $str = @queue.shift) { + return $str if $str.chars ≥ $len; + @queue.push: |$str «~« %edges{ $str.substr(*-1) }; + } + } +} + +#| Print all vowel strings for the specified length +sub MAIN( Int $len where { $len > 0 } = 3 ) { + my $it = vowel-string-iter( $len ); + .say while $_ = $it.(); +} |
