diff options
| author | Ruben Westerberg <drclaw@mac.com> | 2019-12-15 11:46:28 +1000 |
|---|---|---|
| committer | Ruben Westerberg <drclaw@mac.com> | 2019-12-15 11:46:28 +1000 |
| commit | f28bf5b2020f2a0d3056fad061fedc6c21b5848e (patch) | |
| tree | c95721440ab3f057553e050ce0ada285b572ddde /challenge-038 | |
| parent | c3d26cb633e5496c02c25fe9159a1988a012ef44 (diff) | |
| download | perlweeklychallenge-club-f28bf5b2020f2a0d3056fad061fedc6c21b5848e.tar.gz perlweeklychallenge-club-f28bf5b2020f2a0d3056fad061fedc6c21b5848e.tar.bz2 perlweeklychallenge-club-f28bf5b2020f2a0d3056fad061fedc6c21b5848e.zip | |
Added ch-2.p6 updated ch-2.pl
Diffstat (limited to 'challenge-038')
| -rwxr-xr-x | challenge-038/ruben-westerberg/perl/ch-2.pl | 75 | ||||
| -rwxr-xr-x | challenge-038/ruben-westerberg/raku/ch-2.p6 | 40 |
2 files changed, 69 insertions, 46 deletions
diff --git a/challenge-038/ruben-westerberg/perl/ch-2.pl b/challenge-038/ruben-westerberg/perl/ch-2.pl index be0853a2a4..16ddaa32dd 100755 --- a/challenge-038/ruben-westerberg/perl/ch-2.pl +++ b/challenge-038/ruben-westerberg/perl/ch-2.pl @@ -3,70 +3,49 @@ use strict; use warnings; use List::Util; -my %tileBag; #Bag of all tiles -my %values; #Map of letter to value/score - my @l=split "", "AGISUXZEJLRVYFDPWBNTOHMCKQ"; my @c=(8,3,5,7,5,2,5,9,3,3,3,3,5,3,3,5,5,5,4,5,3,3,4,4,2,2); my @v=((1)x7, (2)x6,(3)x4,(4)x2,(5)x5,(10)x2); +my %tileBag; #Bag of all tiles +my %values; #Map of letter to value/score +my %drawBag; #Bag of7 tiles drawn + + +#Build the bags and maps for (0..$#l) { $tileBag{$l[$_]}=$c[$_]; $values{$l[$_]}=$v[$_]; } -my @words=buildValidWordList(); -my $draw=join "", map {drawTile(\%tileBag)} 1..7; -my %contenders=contenderWords($draw,\@words); -print "Contenders (word: score):\n"; -my @sorted=sort { $contenders{$a} <=> $contenders{$b} } keys %contenders; -print "$_: $contenders{$_}\n" for @sorted; -print "\nTiles Drawn: $draw\n"; - -=item contenderWords() -Finds all matching words in valid word list from the drawing letters. -Match is performed by sorting letters of words. A regex is used to then match contender words in the draw word -Returns a hash of the contender words and scores -=cut -sub contenderWords { - my ($draw,$validWordList)=@_; - my %words; - my $w=join "", sort split "", $draw; - for (@$validWordList) { - my @t=sort split "",$_; - my $re=join "",@t; - if($w =~ /$re/) { - my $sum=List::Util::sum(@values{@t}); - $words{$_}=$sum; - } - } - %words; -} - -=item drawTile() -Draws a tile from the tile bag. The bag is adjusted by removing the tile -=cut -sub drawTile { - my $tileBag=shift; - my $total= List::Util::sum(values %$tileBag); +#Draw the 7 tiles. Update bag with removed tile +for (1..7) { + my $total= List::Util::sum(values %tileBag); my $i=int rand($total); my $t=0; my $selected; for ("A".."Z") { - $t+=$$tileBag{$_}; + $t+=$tileBag{$_}; if ($t>$i) { - $$tileBag{$_}--; - $selected=$_; + $tileBag{$_}--; + $drawBag{$_}++; last; } } - $selected; } -=item buildValidWordList -Returns a list of words which are up to 7 letters long and which do not exceed the available tiles -=cut -sub buildValidWordList { +#Find all words which can be made from the drawn bag +my %contenders=map {($_, List::Util::sum( @values{split ""}))} possibleWords(); + +#Print sores of all possible words in asscending order +my @sorted=sort { $contenders{$a} <=> $contenders{$b} } keys %contenders; +print "Contenders (word: score):\n"; +print "$_: $contenders{$_}\n" for @sorted; +print "\nTiles Drawn: ",join(", ", map({($_) x $drawBag{$_}} keys %drawBag)),"\n"; + + +#Helper sub to test known words against drawn tiles +sub possibleWords { open my $f, "<","../words_alpha.txt"; my @words= grep { chomp; length($_) <= 7} map {uc} <$f>; grep { my %bag; @@ -75,7 +54,11 @@ sub buildValidWordList { }; my $valid=1; for (keys %bag) { - $valid&=($bag{$_}<=$tileBag{$_}); + if (defined $drawBag{$_}) { + $valid&=($bag{$_}<=$drawBag{$_}); + next; + } + $valid&=0; } $valid } @words; diff --git a/challenge-038/ruben-westerberg/raku/ch-2.p6 b/challenge-038/ruben-westerberg/raku/ch-2.p6 new file mode 100755 index 0000000000..66c03332a6 --- /dev/null +++ b/challenge-038/ruben-westerberg/raku/ch-2.p6 @@ -0,0 +1,40 @@ +#!/usr/bin/env perl6 + +#Input data +my @l=comb "", "AGISUXZEJLRVYFDPWBNTOHMCKQ"; +my @c=(8,3,5,7,5,2,5,9,3,3,3,3,5,3,3,5,5,5,4,5,3,3,4,4,2,2); +my @v=((1) xx 7, (2) xx 6,(3) xx 4,(4) xx 2,(5) xx 5,(10) xx 2).flat; + +#Build bag and map structures +my BagHash $tileBag.=new-from-pairs((@l Z @c).flat.pairup); #Bag of all tiles +my %values=(@l Z @v).flat; #Letter value map +my BagHash $drawBag; #Bag of drawn tiles + +#Draw the 7 tiles. Update bag with removed tile +for 1..7 { + my $i=$tileBag.total.rand.Int; + my $t=0; + my $selected; + for "A".."Z" { + $t+=$tileBag{$_}; + if $t > $i { + $tileBag{$_}--; + $drawBag{$_}++; + last; + } + } +} + +#Find all words which can be made from the drawn bag +"../words_alpha.txt".IO.lines.map({.uc}).grep({$_.chars <= 7}) ==> +grep({ .comb.BagHash (<=) $drawBag; }) ==> my @contenders; + +#Print sores of all possible words in asscending order +my %contenders=@contenders.map({|($_, %values{.comb}.sum)}); +my @sorted=%contenders.keys.sort({%contenders{$^a} <=> %contenders{$^b}}); + +put "Contenders (word: score):"; +for @sorted { + put "$_: %contenders{$_}" +} +put "\nDrawn tiles: $drawBag"; |
