-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
103 lines (82 loc) · 2.94 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WordleReverseSolver;
//Console.WriteLine($"Starting at {DateTime.Now}");
Console.WriteLine("Reading in dictionary...");
string dictionaryJson = File.ReadAllText("dictionary.json");
var jarray = (JArray)JsonConvert.DeserializeObject(dictionaryJson);
var allWords = jarray.ToObject<string[]>();
Console.WriteLine($"Dictionary loaded with {allWords.Length} words.");
Console.WriteLine();
int puzzleNumber = int.Parse(args[0]);
Console.WriteLine($"Searching and parsing tweets for Wordle puzzle {puzzleNumber}...");
var mainPatternManager = await PatternManager.ReadFromTwitter(puzzleNumber);
Console.WriteLine($"Found {mainPatternManager.Count} distinct patterns across {mainPatternManager.TweetCount} tweets.");
Console.WriteLine();
//DumpMismatchedPatternItems("hello");
//return;
Console.WriteLine("Searching for most likely solutions (in order):");
var results = allWords
.AsParallel()
.Select(candidateSolution => new Tuple<string, int>(candidateSolution, GetMismatchedPatternIncidenceCount(candidateSolution)))
//.Where(pair => pair.Item2 < 5)
.OrderBy(pair => pair.Item2).Take(5);
foreach (var pair in results)
{
Console.WriteLine($"{pair.Item1} ({pair.Item2})");
//Console.WriteLine($"{pair.Item1}");
}
//Console.WriteLine($"Ending at {DateTime.Now}");
// Get the total incidence count patterns that are impossible for this word
int GetMismatchedPatternIncidenceCount(string candidateSolution)
{
var patternManager = (PatternManager)mainPatternManager.Clone();
for (int guess = 0; guess < allWords.Length; guess++)
{
var pattern = ScoreWord(allWords[guess], candidateSolution);
patternManager.RemovePattern(pattern);
}
return patternManager.Count;
}
// For debugging purpose
void DumpMismatchedPatternItems(string candidateSolution)
{
var patternManager = (PatternManager)mainPatternManager.Clone();
for (int guess = 0; guess < allWords.Length; guess++)
{
var pattern = ScoreWord(allWords[guess], candidateSolution);
patternManager.RemovePattern(pattern);
}
patternManager.DumpAllPatterns();
}
int[] ScoreWord(string guess, string solution)
{
var used = new bool[solution.Length];
var score = new int[solution.Length];
// Go through letters in the guess
for (int i = 0; i < solution.Length; i++)
{
// Match in correct position
if (guess[i] == solution[i])
{
used[i] = true;
score[i] = 2;
continue;
}
// Look for match in an incorrect position
for (int j = 0; j < solution.Length; j++)
{
if (!used[j])
{
if (guess[i] == solution[j] && guess[j] != solution[j])
{
used[j] = true;
score[i] = 1;
break;
}
}
}
// Leave as 0 if no match
}
return score;
}