From a24bb308923a7646ca635b96eb4bd0d55986f6be Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Thu, 13 Jan 2022 20:35:29 -0500 Subject: instead of calculating word value every time sort is called, precalculate hash --- wordle.pl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/wordle.pl b/wordle.pl index f6178be..7b67fd1 100644 --- a/wordle.pl +++ b/wordle.pl @@ -37,6 +37,11 @@ my $total = scalar @words; @words = grep( {$_ =~ /^.{$wlen}$/} @words); say "total $wlen letter words: " . scalar @words . "\n"; +my %wordvalues; +say "building wordvalues\n"; +for (@words) { + $wordvalues{$_} = wordvalue($_); +} # In word letters my %iwl; # letters in correct position @@ -84,10 +89,10 @@ while($guess ne $answer ) { say "filtering letter duplicates for first guess:" . scalar @good_start_words; - @good_start_words = reverse sort wordvalue @good_start_words; + @good_start_words = reverse sort wordvalue_sort @good_start_words; $guess = $good_start_words[0]; - say "picking $guess (score:" . lvalue($guess) . ") because it scores highest in letter frequency.\n"; + say "picking $guess (score:" . wordvalue($guess) . ") because it scores highest in letter frequency.\n"; say " let's go!"; say " -------------------"; } else { @@ -259,7 +264,7 @@ sub uniqletters { # okay so the premise here is to create a sort based on the frequency of a a # letter... higher frequnency = higher initial hit = higher sort order. -sub lvalue { +sub wordvalue { my $w = shift; # my $freq = "etaoinsrhdlucmfywgpbvkxqjz"; @@ -276,9 +281,10 @@ sub lvalue { } # i stole this from stackoverflow. -sub wordvalue { - $av = lvalue($a); - $bv = lvalue($b); +sub wordvalue_sort { + # use a lookup table. + $av = $wordvalues{$a}; + $bv = $wordvalues{$b}; if($av < $bv){ return -1; -- cgit v1.2.1