summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCalvin Morrison <calvin@fastmailteam.com>2022-01-13 21:21:08 -0500
committerCalvin Morrison <calvin@fastmailteam.com>2022-01-13 21:21:08 -0500
commit8aeb52112031190e72ecb49336c5f4dd14978d73 (patch)
tree0fcdd7a32266d7d79061128013686e136b0b7c30
parenta24bb308923a7646ca635b96eb4bd0d55986f6be (diff)
calculate frequencies on the fly, fix bug in our char match regex
-rw-r--r--wordle.pl29
1 files changed, 24 insertions, 5 deletions
diff --git a/wordle.pl b/wordle.pl
index 7b67fd1..a44026c 100644
--- a/wordle.pl
+++ b/wordle.pl
@@ -33,15 +33,20 @@ my $wlen = length($answer);
say "total dict size: " . scalar @words;
my $total = scalar @words;
-# filter to word length
+
@words = grep( {$_ =~ /^.{$wlen}$/} @words);
say "total $wlen letter words: " . scalar @words . "\n";
+my $frequency = letter_frequency(@words);
+say "calc: $frequency";
+
+# generate word values ahead of time as a cache for sort.
my %wordvalues;
say "building wordvalues\n";
for (@words) {
$wordvalues{$_} = wordvalue($_);
}
+
# In word letters
my %iwl;
# letters in correct position
@@ -178,8 +183,9 @@ while($guess ne $answer ) {
# filter for letters in the answer only
if(scalar keys %iwl) {
- my $pattern = '(' . join ('|', sort keys %iwl) . ')';
- @words = grep { $_ =~ /$pattern/ } @words;
+ for my $c(keys %iwl) {
+ @words = grep { $_ =~ /$c/ } @words;
+ }
}
print scalar @words;
# sleep 1;
@@ -269,13 +275,15 @@ sub wordvalue {
# my $freq = "etaoinsrhdlucmfywgpbvkxqjz";
# reverse frequency, later numbers have higher value
- my $f= "zjqxkvbpgwyfmculdhrsnioate";
+ # general language english:
+ # this should be calculated by length
+ # my $f = "zjqxkvbpgwyfmculdhrsnioate";
my $v = 1;
for my $c (split('', $w)) {
# this is basically bullshit, but sum the value against the index
# position...
- $v += (index($f, $c));
+ $v += (index($frequency, $c));
}
return $v;
}
@@ -294,3 +302,14 @@ sub wordvalue_sort {
return 1;
}
}
+
+sub letter_frequency {
+ my $charfreqhash;
+ for (@_) {
+ for $char (split('', $_)) {
+ $charfreqhash{$char}++;
+ }
+ }
+ my @char_frequency = reverse sort { $charfreqhash{$b} <=> $charfreqhash{$a} } keys %charfreqhash;
+ return join('', @char_frequency);
+}