diff options
author | Calvin Morrison <calvin@fastmailteam.com> | 2022-01-13 20:35:29 -0500 |
---|---|---|
committer | Calvin Morrison <calvin@fastmailteam.com> | 2022-01-13 20:35:29 -0500 |
commit | a24bb308923a7646ca635b96eb4bd0d55986f6be (patch) | |
tree | 1d3ba1e6e574b0f52f0fe968836ed096f14c0881 | |
parent | 014166ef46e2cf9c0d00159aa8a07d69f8d37c35 (diff) |
instead of calculating word value every time sort is called, precalculate hash
-rw-r--r-- | wordle.pl | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -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; |