From 4ffc08e38b9e274f453ba25fe53cad502ac02b35 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sat, 14 Dec 2019 16:50:01 +1000 Subject: Added ch-1.pl --- challenge-038/ruben-westerberg/perl/ch-1.pl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 challenge-038/ruben-westerberg/perl/ch-1.pl (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/perl/ch-1.pl b/challenge-038/ruben-westerberg/perl/ch-1.pl new file mode 100755 index 0000000000..7723ce2c0a --- /dev/null +++ b/challenge-038/ruben-westerberg/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my $d= qr(@{[join "|", map { sprintf "%02d", $_} 1..31]}); +my $m= qr(@{[join "|", map { sprintf "%02d", $_} 1..12]}); + +for (@ARGV) { + if (/(1|2)([0-9]{2})($m)($d)/) { + print "Input $_ OK\n"; + print(join("-",($1==1?"20$2":"19$2",$3,$4)),"\n"); + next; + } + print "Input $_ invalid\n"; +} -- cgit From 1944cb91286c89585e377109a866b9d36cf96c91 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sat, 14 Dec 2019 23:08:45 +1000 Subject: Added ch-2.pl --- challenge-038/ruben-westerberg/perl/ch-2.pl | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 challenge-038/ruben-westerberg/perl/ch-2.pl (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/perl/ch-2.pl b/challenge-038/ruben-westerberg/perl/ch-2.pl new file mode 100755 index 0000000000..9b533aa8eb --- /dev/null +++ b/challenge-038/ruben-westerberg/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!/usr/bin/env perl +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); + +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 +Returns a hash of the contender word and it score +=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); + my $i=int rand($total); + my $t=0; + my $selected; + for ("A".."Z") { + $t+=$$tileBag{$_}; + if ($t>$i) { + $$tileBag{$_}--; + $selected=$_; + 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 { + open my $f, "<","../words_alpha.txt"; + my @words= grep { chomp; length($_) <= 7} map {uc} <$f>; + grep { my %bag; + for (split "") { + $bag{$_}++ + }; + my $valid=1; + for (keys %bag) { + $valid&=($bag{$_}<=$tileBag{$_}); + } + $valid + } @words; + +} -- cgit From c3d26cb633e5496c02c25fe9159a1988a012ef44 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sat, 14 Dec 2019 23:13:14 +1000 Subject: Updated comments --- challenge-038/ruben-westerberg/perl/ch-2.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/perl/ch-2.pl b/challenge-038/ruben-westerberg/perl/ch-2.pl index 9b533aa8eb..be0853a2a4 100755 --- a/challenge-038/ruben-westerberg/perl/ch-2.pl +++ b/challenge-038/ruben-westerberg/perl/ch-2.pl @@ -14,6 +14,7 @@ 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); @@ -23,8 +24,9 @@ print "$_: $contenders{$_}\n" for @sorted; print "\nTiles Drawn: $draw\n"; =item contenderWords() -Finds all matching words in valid word list from the drawing letters -Returns a hash of the contender word and it score +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)=@_; @@ -44,7 +46,6 @@ sub contenderWords { =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); @@ -78,5 +79,4 @@ sub buildValidWordList { } $valid } @words; - } -- cgit From f28bf5b2020f2a0d3056fad061fedc6c21b5848e Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 15 Dec 2019 11:46:28 +1000 Subject: Added ch-2.p6 updated ch-2.pl --- challenge-038/ruben-westerberg/perl/ch-2.pl | 75 +++++++++++------------------ challenge-038/ruben-westerberg/raku/ch-2.p6 | 40 +++++++++++++++ 2 files changed, 69 insertions(+), 46 deletions(-) create mode 100755 challenge-038/ruben-westerberg/raku/ch-2.p6 (limited to 'challenge-038') 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"; -- cgit From 646eb9134b417c19f5df180ae57cc4b27b32572d Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 15 Dec 2019 11:49:07 +1000 Subject: Added README --- challenge-038/ruben-westerberg/README | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/README b/challenge-038/ruben-westerberg/README index 7c64cafc0c..eabb2c8160 100644 --- a/challenge-038/ruben-westerberg/README +++ b/challenge-038/ruben-westerberg/README @@ -2,13 +2,11 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.p6 =================== -Run the program to display the number of weekdays in the months of 2019 +Run the program with at least 1 command line argument (7 digit number) to validated and generate a date for each argument. ch-2.pl and ch-2.p6 =================== -Run the program to show the amount of daylight duration difference between November and December -Note for Raku version the HTTP::UserAgent module is required - +Run the program to randomly select 7 tiles from a bag and display the possible words and associated score which could be created from the 7 tiles -- cgit From 920894139d0b61046a407989374499cf59a135d8 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 15 Dec 2019 11:50:02 +1000 Subject: Added ch-1.p6 --- challenge-038/ruben-westerberg/raku/ch-1.p6 | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100755 challenge-038/ruben-westerberg/raku/ch-1.p6 (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/raku/ch-1.p6 b/challenge-038/ruben-westerberg/raku/ch-1.p6 new file mode 100755 index 0000000000..b601c651e4 --- /dev/null +++ b/challenge-038/ruben-westerberg/raku/ch-1.p6 @@ -0,0 +1,13 @@ +#!/usr/bin/env perl6 +my $m=(1..12)>>.fmt("%02d").join("|"); +my $d=(1..31)>>.fmt("%02d").join("|"); + +for @*ARGS { + if /(1|2)(<[0..9]>**2)(<$m>)(<$d>)/ { + put "Input $_ OK"; + put ($0==1??"20$1"!!"19$1",$2,$3).join("-"); + next; + } + print "Input $_ invalid"; +} + -- cgit From 8e77ea6b8a3d50892e3f1b7517fa4560ee01cea9 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 15 Dec 2019 11:55:21 +1000 Subject: Added feed another feed operator Skips intermeadiate variable usage --- challenge-038/ruben-westerberg/raku/ch-2.p6 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'challenge-038') diff --git a/challenge-038/ruben-westerberg/raku/ch-2.p6 b/challenge-038/ruben-westerberg/raku/ch-2.p6 index 66c03332a6..e7927c5d67 100755 --- a/challenge-038/ruben-westerberg/raku/ch-2.p6 +++ b/challenge-038/ruben-westerberg/raku/ch-2.p6 @@ -27,14 +27,13 @@ for 1..7 { #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; +grep({ .comb.BagHash (<=) $drawBag; }) ==> +map({|($_, %values{.comb}.sum)})==> +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 "$_: %contenders{$_}" for @sorted; put "\nDrawn tiles: $drawBag"; -- cgit