Initial commit — Wordle/letter word solver and scorer

This commit is contained in:
2026-05-10 03:01:15 +00:00
commit 900ace491f
41 changed files with 391999 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{87245554-C14C-4760-9AD8-7948F6506522}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Core</RootNamespace>
<AssemblyName>Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="C5">
<HintPath>..\References\C5.dll</HintPath>
</Reference>
<Reference Include="MathNet.Numerics, Version=4.15.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MathNet.Numerics.4.15.0\lib\net40\MathNet.Numerics.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DictionarySearcher.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="JangleWordScorer.cs" />
<Compile Include="LetterPositionFrequency.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WordFrequency.cs" />
<Compile Include="WordleUtil.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="all.num.o5.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TWL06.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="WordleWords.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+146
View File
@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using C5;
namespace Core
{
public class DictionarySearcher
{
static HashBag<char> GetLettersBag(string letters)
{
var hashBag = new HashBag<char>();
hashBag.AddAll(letters.Canonicalize().ToCharArray());
return hashBag;
}
readonly struct DictWord
{
public readonly string Word;
public readonly HashBag<char> CharBag;
public DictWord(string word) : this()
{
Word = word;
CharBag = GetLettersBag(word);
}
public static implicit operator DictWord(string s) => new DictWord(s);
public static implicit operator string(DictWord dw) => dw.Word;
public char[] ToCharArray() => Word.ToCharArray();
public bool Contains(char c) => Word.Contains(c);
public int Length => Word.Length;
public bool StartsWith(string s) => Word.StartsWith(s);
public string Substring(int startIndex) => Word.Substring(startIndex);
}
private class DictWordComprer : IComparer<DictWord>
{
private readonly IComparer<string> _comparer;
public DictWordComprer(IComparer<string> comparer)
{
_comparer = comparer;
}
public int Compare(DictWord x, DictWord y)
{
return _comparer.Compare(x, y);
}
}
private readonly List<DictWord> _dictionary;
public DictionarySearcher(int? characterCount = null)
: this(@"TWL06.txt", characterCount)
{
}
public DictionarySearcher(string filename, int? characterCount = null)
: this(filename.GetFileLines(), characterCount)
{
}
public DictionarySearcher(IEnumerable<string> words, int? characterCount = null)
{
_dictionary = new List<DictWord>();
foreach (var word in words)
{
if (characterCount != null && word.Length != characterCount)
continue;
var w = word.Canonicalize();
_dictionary.Add(w);
}
}
public System.Collections.Generic.IList<string> GetWords()
{
return _dictionary.Select(d => d.Word).ToList();
}
public void SortDictionary(IComparer<string> comparer)
{
_dictionary.Sort(new DictWordComprer(comparer));
}
public IEnumerable<string> FindWords(string letters)
{
var hashBag = new HashBag<char>();
hashBag.AddAll(letters.Canonicalize().ToCharArray());
return from w in _dictionary
where hashBag.ContainsAll(w.ToCharArray())
select w.Word;
}
public IEnumerable<string> FindWords(string includeLetters, string excludeLetters)
{
var excludeBag = GetLettersBag(excludeLetters);
return from w in _dictionary
where w.CharBag.ContainsAll(includeLetters.Canonicalize())
&& excludeBag.All(c => !w.Contains(c))
select w.Word;
}
public IEnumerable<string> FindWords(string includeLetters, string excludeLetters, int? len)
{
return
from w in FindWords(includeLetters, excludeLetters)
where !len.HasValue || w.Length == len.Value
select w;
}
public IEnumerable<Tuple<string, string>> MatchingWords(string firstSelector, string secondSelector)
{
var firstSet = _dictionary.Where(w => w.StartsWith(firstSelector.Canonicalize())).GroupBy(w => w.Length)
.ToList();
var secondSet = _dictionary.Where(w => w.StartsWith(secondSelector.Canonicalize())).GroupBy(w => w.Length)
.ToList();
return from len in firstSet.Select(g => g.Key)
from w1 in firstSet.Where(g => g.Key == len)
from w2 in secondSet.Where(g => g.Key == len)
from ww1 in w1
from ww2 in w2
where ww1.Substring(1).Equals(ww2.Substring(1))
select Tuple.Create(ww1.Word, ww2.Word);
}
/*public Dictionary<char, int>[] GetLetterPositionFreq(int wordLen)
{
var ws = _dictionary.Where(w => w.Length == wordLen).ToList();
var freqList = new Dictionary<char, int>[wordLen];
for (var i = 0; i < wordLen; i++)
{
var letters = Enumerable.Range(0, 26).Select(r => Convert.ToChar('A' + r));
freqList[i] =
letters.ToDictionary(c => c,
c => ws.Count(w => w.Word[i] == c)
);
}
return freqList;
}*/
}
}
+27
View File
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.IO;
namespace Core
{
public static class Extensions
{
public static string Canonicalize(this string s)
{
return s.ToUpperInvariant();
}
public static IEnumerable<string> GetFileLines(this string filename)
{
using (var sr = File.OpenText(filename))
{
while (true)
{
var line = sr.ReadLine();
if (line == null)
yield break;
yield return line;
}
}
}
}
}
+57
View File
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
namespace Core
{
public static class JangleWordScorer
{
private static readonly IDictionary<char, int> _scores = new Dictionary<char, int>(26);
static JangleWordScorer()
{
foreach (var ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
switch (ch)
{
case 'Z':
case 'J':
case 'Q':
case 'K':
case 'X':
_scores.Add(ch, 3);
break;
case 'H':
case 'B':
case 'Y':
case 'M':
case 'C':
case 'F':
case 'W':
_scores.Add(ch, 2);
break;
default:
_scores.Add(ch, 1);
break;
}
}
public static int Score(string str)
{
return
(from c in str.Canonicalize().ToCharArray()
let score = _scores[c]
select score).Sum();
}
public static IDictionary<int, IEnumerable<string>> GroupByScore(IEnumerable<string> st)
{
return
(from s in st
let score = Score(s)
group s by score
into g
orderby g.Key descending
let vals = g.Select(v => v)
select new {g.Key, vals}).ToDictionary(d => d.Key, d => d.vals);
}
}
}
+83
View File
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Core
{
public class LetterPositionFrequency : IComparer<string>
{
private readonly Dictionary<char, int>[] _letterFrequencies;
private LetterPositionFrequency(Dictionary<char, int>[] letterFrequencies)
{
_letterFrequencies = letterFrequencies;
}
public LetterPositionFrequency(DictionarySearcher dictionarySearcher, int wordLen)
: this(GetLetterPositionFreq(dictionarySearcher.GetWords(), wordLen))
{
}
public LetterPositionFrequency(WordleUtil wordleUtil, int wordLen)
: this(GetLetterPositionFreq(wordleUtil.Words, wordLen))
{
}
public LetterPositionFrequency(IList<string> words, int wordLen)
: this(GetLetterPositionFreq(words, wordLen))
{
}
public static Dictionary<char, int>[] GetLetterPositionFreq(IList<string> words, int wordLen)
{
var ws = words.Where(w => w.Length == wordLen).ToList();
var freqList = new Dictionary<char, int>[wordLen];
for (var i = 0; i < wordLen; i++)
{
var letters = Enumerable.Range(0, 26).Select(r => Convert.ToChar('A' + r));
freqList[i] =
letters.ToDictionary(c => c,
c => ws.Count(w => w[i] == c)
);
}
return freqList;
}
public IEnumerable<Tuple<string, int>> ScoreWords(IEnumerable<string> words, WordleUtil wordleUtil = null)
{
return
from word in words
let score =
word.Select((c, i) => _letterFrequencies[i][c]).Sum()
select
wordleUtil != null
? Tuple.Create(word, Convert.ToInt32(wordleUtil.FactorRepeatedLetters(word, score)))
: Tuple.Create(word, score)
;
}
public List<string> SortWords(IEnumerable<string> words, WordleUtil wordleUtil = null)
{
return ScoreWords(words, wordleUtil)
.OrderByDescending(w => w.Item2)
.Select(w => w.Item1)
.ToList();
}
private int ScoreWord(string word)
{
return word.Select((c, i) => _letterFrequencies[i][c]).Sum();
}
public int Compare(string x, string y)
{
return ScoreWord(y).CompareTo(ScoreWord(x));
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Core")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("40731b2b-b149-4112-887f-21941a4272a0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+178691
View File
File diff suppressed because it is too large Load Diff
+51
View File
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Core
{
public class WordFrequency : IComparer<string>
{
private readonly Dictionary<string, int> _dictionary;
public WordFrequency()
: this(@"all.num.o5.txt")
{
}
private static IEnumerable<Tuple<string, int>> ParseWordFreq(IEnumerable<string> lines)
{
var regex = new Regex("[a-z]+");
return
from line in lines
select line.Split(' ')
into fields
where fields.Length == 4 && regex.Match(fields[1]).Success
let word = fields[1]
let freq = int.Parse(fields[0])
select new Tuple<string, int>(word.Canonicalize(), freq);
}
public WordFrequency(string filename)
: this(ParseWordFreq(filename.GetFileLines()))
{
}
public WordFrequency(IEnumerable<Tuple<string, int>> words)
{
_dictionary = new Dictionary<string, int>();
foreach (var wf in words.Where(wf => !_dictionary.ContainsKey(wf.Item1)))
_dictionary.Add(wf.Item1, wf.Item2);
}
public int GetWordFreq(string word)
{
return _dictionary.ContainsKey(word) ? _dictionary[word] : 0;
}
public int Compare(string x, string y)
{
return _dictionary[x].CompareTo(_dictionary[y]);
}
}
}
+188
View File
@@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Core
{
public class WordleUtil
{
public enum CharacterResponse
{
Actual,
OtherPosition,
NotInWord
}
public struct PositionCriteria
{
public char? Correct;
public string OtherPosition;
public string GetRegexRestriction() =>
Correct.HasValue
? Correct.Value.ToString()
: !string.IsNullOrEmpty(OtherPosition)
? "[^" + OtherPosition + "]"
: ".";
public void RemoveLetter(char letter)
{
OtherPosition = OtherPosition?.Replace(letter.ToString(), string.Empty);
}
}
public class WordleState
{
public string InWord { get; private set; } = string.Empty;
public string NotInWord { get; private set; } = string.Empty;
public PositionCriteria[] Criteria { get; } = new PositionCriteria[5];
public bool IsSolved() => Criteria.All(c => c.Correct.HasValue);
private void AddInWord(char letter)
{
if (!InWord.Contains(letter))
InWord += letter;
}
private void AddNotInWord(char letter)
{
if (!NotInWord.Contains(letter))
NotInWord += letter;
}
public void Update(string word, CharacterResponse[] response)
{
for (int i = 0; i < 5; i++)
{
var letter = word[i];
switch (response[i])
{
case CharacterResponse.Actual:
// remove this letter from the criteria
foreach (var positionCriteria in Criteria)
positionCriteria.RemoveLetter(letter);
AddInWord(letter);
Criteria[i].Correct = letter;
break;
case CharacterResponse.OtherPosition:
AddInWord(letter);
Criteria[i].OtherPosition += letter;
break;
case CharacterResponse.NotInWord:
AddNotInWord(letter);
break;
}
}
}
}
public static string GetRegexRestrictions(PositionCriteria[] criteria)
{
return
criteria
.Aggregate(string.Empty,
(r, criterion)
=> r + criterion.GetRegexRestriction()
);
}
public IList<string> Words { get; }
private Dictionary<int, double> _repeatedLetterFactor;
public WordleUtil()
: this(@"WordleWords.txt")
{
}
public WordleUtil(string filename)
: this(filename.GetFileLines())
{
}
public WordleUtil(IEnumerable<string> words)
{
Words = new List<string>();
foreach (var word in words)
{
var w = word.Canonicalize();
var split = w.Split(',');
Words.Add(split[0]);
}
Words.Remove("ADMIN");
Words.Remove("INBOX");
var re =
from w in Words
let repeats = RepeatedLettersCount(w)
group repeats by repeats
into g
select g;
var max = re.Max(r => r.Count());
_repeatedLetterFactor =
re.Select(r => Tuple.Create(r.Key, r.Count() * 1.0 / max))
.ToDictionary(s => s.Item1, s => s.Item2);
}
public static CharacterResponse[] GetGuessResult(string actual, string guess)
{
if (actual.Length != 5 || guess.Length != 5)
throw new ArgumentOutOfRangeException("not right size word or guess");
var response = new CharacterResponse[5];
for (var i = 0; i < 5; i++)
{
if (guess[i] == actual[i])
response[i] = CharacterResponse.Actual;
else if (actual.Contains(guess[i]))
response[i] = CharacterResponse.OtherPosition;
else
response[i] = CharacterResponse.NotInWord;
}
return response;
}
public static bool RepeatedLetters(string word)
{
var uniqueLetters = new HashSet<char>();
foreach (var c in word)
uniqueLetters.Add(c);
return uniqueLetters.Count < word.Length;
}
public static int RepeatedLettersCount(string word)
{
var uniqueLetters = new HashSet<char>();
foreach (var c in word)
uniqueLetters.Add(c);
return word.Length - uniqueLetters.Count;
}
public double FactorRepeatedLetters(string word, double score)
{
return score * _repeatedLetterFactor[RepeatedLettersCount(word)];
}
public Dictionary<char, int>[] GetLetterPositionFreq(int wordLen)
{
var ws = Words.Where(w => w.Length == wordLen).ToList();
var freqList = new Dictionary<char, int>[wordLen];
for (var i = 0; i < wordLen; i++)
{
var letters = Enumerable.Range(0, 26).Select(r => Convert.ToChar('A' + r));
freqList[i] =
letters.ToDictionary(c => c,
c => ws.Count(w => w[i] == c)
);
}
return freqList;
}
}
}
+2316
View File
File diff suppressed because it is too large Load Diff
+208657
View File
File diff suppressed because it is too large Load Diff
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MathNet.Numerics" version="4.15.0" targetFramework="net40" />
</packages>