This repository was archived by the owner on Dec 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
323 lines (322 loc) · 12.2 KB
/
Program.cs
File metadata and controls
323 lines (322 loc) · 12.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Numerics;
using System.Xml;
namespace Wordler
{
internal class Program
{
static string[] words;
static int maxlen = 0;
static Dictionary<string, int> wordbank;
static string[] ogargs;
static string somewhere = "";
static string weightfile = "";
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("NOTE: When you input wordle response; green: 2, yellow: 1, gray: 0");
Console.WriteLine("So if you got green-gray-gray-yellow-gray, type 20010\n");
ogargs = args;
Console.WriteLine("Obtaining Data...");
string input = "";
if (args.Length > 0)
{
if (File.Exists(args[0]))
{
input = File.ReadAllText(args[0]);
if (args.Length > 1)
{
if (File.Exists(args[1]))
{
weightfile = args[1];
}
}
}
else
{
Console.WriteLine("Invalid file!");
Console.ReadKey();
Environment.Exit(1);
}
}
else
{
if (!File.Exists("input.txt"))
{
Console.WriteLine("Missing input.txt!");
Console.WriteLine("Try passing your input file as a parameter.");
Console.ReadKey();
Environment.Exit(2);
}
input = File.ReadAllText("input.txt").ToLower().Trim();
}
Console.WriteLine("Sorting Data...");
words = input.ToLower().Trim().Split('\n');
maxlen = words[0].Trim().Length;
foreach (string word in words)
{
if (word.Trim().Length != maxlen)
{
Console.WriteLine("Word length inconsistency!");
Console.ReadKey();
Environment.Exit(3);
}
}
wordbank = words
.Select((str, index) => new { Key = str.Trim(), Value = 0 })
.ToDictionary(pair => pair.Key, pair => pair.Value);
if (weightfile == "")
{
CalculateWeights();
}
else
{
LoadWeights();
}
}
static void CalculateWeights()
{
Console.Write("Weighing Data... ");
wordbank.Where(word => !somewhere.All(letter => word.Key.Contains(letter)))
.ToList().ForEach(kvp => wordbank.Remove(kvp.Key));
somewhere = "";
int index = 0;
foreach (string myguess in wordbank.Keys)
{
int score = 0;
foreach (string slot in wordbank.Keys)
{
string used = "";
for (int i = 0; i < maxlen; i++)
{
if (used.Contains(myguess[i]))
{
continue;
}
if (slot[i] == myguess[i])
{
score += 3;
}
else if (slot.Contains(myguess[i]))
{
score += 1;
}
used = used + myguess[i];
}
}
wordbank[myguess] = score;
Console.Write($"\rWeighing Data... ({Math.Round(index * 100 / (double)wordbank.Count())}%)");
index++;
}
Console.WriteLine("\rWeighing Data... Done. ");
Console.WriteLine("Sorting Dictionary...");
wordbank = wordbank.OrderByDescending(x => x.Value)
.ToDictionary(x => x.Key, x => x.Value);
Guess();
}
static void Guess(string input = "")
{
KeyValuePair<string, int> guess = new KeyValuePair<string, int>("", 0);
try
{
if (wordbank.ContainsKey(input) && input != "")
{
guess = wordbank.Where(x => x.Key == input).First();
}
else
{
guess = wordbank.First();
}
}
catch
{
Console.WriteLine("Hmmm... no more words...");
Console.WriteLine("Please confirm that the word is contained within the input file.");
}
Console.WriteLine($"\nGuess: {guess.Key.ToUpper()}\n");
if (wordbank.Count != 1)
{
Console.WriteLine($"Words remaining: {(BigInteger)wordbank.Count} | Guess score: {Math.Round(guess.Value * 100 / (double)wordbank.Sum(x => x.Value), 2)}%");
}
else
{
{
Console.WriteLine("GG! Press any key to play again.");
Console.ReadKey();
Main(ogargs);
}
}
bool ok = false;
string? result = "";
while (!ok)
{
Console.Write("\nResult: ");
result = Console.ReadLine()?.Trim();
if (result == null)
{
Console.WriteLine("Please type result.");
continue;
}
if (result == "save")
{
SaveWeights();
continue;
}
bool invalid = false;
foreach (char c in result)
{
if (c != '0' && c != '1' && c != '2')
{
invalid = true;
}
}
if (invalid)
{
int len = wordbank.First().Key.Length;
int countlen = wordbank.First().Value.ToString().Length;
if (result == "@list")
{
Console.WriteLine(printLine(len, countlen));
foreach (KeyValuePair<string, int> word in wordbank)
{
Console.WriteLine($"Word: {word.Key} | Guess score: {Math.Round(word.Value * 100 / (double)wordbank.Sum(x => x.Value), 2).ToString("F2")}% | Guess value: {word.Value}");
}
Console.WriteLine(printLine(len, countlen));
Guess();
return;
}
else if (result.StartsWith("@list"))
{
int head = 0;
int.TryParse(result.Replace("@list", ""), out head);
Console.WriteLine(printLine(len, countlen));
for (int i = 0; i < head && i < wordbank.Count(); i++)
{
KeyValuePair<string, int> word = wordbank.ElementAt(i);
Console.WriteLine($"Word: {word.Key} | Guess score: {Math.Round(word.Value * 100 / (double)wordbank.Sum(x => x.Value), 2).ToString("F2")}% | Guess value: {word.Value}");
}
Console.WriteLine(printLine(len, countlen));
Guess();
return;
}
else if (wordbank.ContainsKey(result) && result != "")
{
Guess(result);
return;
}
Console.WriteLine("Invalid result.");
continue;
}
if (result?.Length != maxlen)
{
Console.WriteLine("Invalid length.");
continue;
}
ok = true;
}
if (result.All(c => c == '2'))
{
Main(ogargs);
return;
}
Console.WriteLine("Removing Redundancies...");
wordbank.Remove(guess.Key);
string used = "";
for (int i = 0; i < maxlen; i++)
{
if (used.Contains(guess.Key[i]))
{
continue;
}
if (result[i] == '2')
{
//wordbank = wordbank.Where(kvp => !kvp.Key[i].Equals(result[i])).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
wordbank.Where(x => !x.Key[i].Equals(guess.Key[i])).ToList()
.ForEach(kvp => wordbank.Remove(kvp.Key));
}
else if (result[i] == '1')
{
wordbank.Where(x => x.Key[i].Equals(guess.Key[i])).ToList()
.ForEach(kvp => wordbank.Remove(kvp.Key));
wordbank.Where(x => !x.Key.Contains(guess.Key[i])).ToList()
.ForEach(kvp => wordbank.Remove(kvp.Key));
somewhere = somewhere + guess.Key[i];
}
else if (result[i] == '0')
{
bool found = false;
if (guess.Key != guess.Key.Distinct().ToString())
{
for (int j = 0; j < maxlen; j++)
{
if (guess.Key[j] == guess.Key[i] && j != i)
{
found = true;
}
}
}
if (found) { continue; }
//wordbank = wordbank.Where(kvp => !kvp.Key.Contains(result[i])).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
wordbank.Where(x => x.Key.Contains(guess.Key[i])).ToList()
.ForEach(kvp => wordbank.Remove(kvp.Key));
}
used = used + guess.Key[i];
}
CalculateWeights();
}
static void LoadWeights()
{
string input = File.ReadAllText(weightfile).Trim();
int[] weights = input.Split('\n').Select(x => Convert.ToInt32(x.Trim())).ToArray();
if (weights.Count() != words.Count())
{
Console.WriteLine("Weight/Input file mismatch.");
Console.WriteLine($"{words.Count()}, {weights.Count()}");
Console.ReadKey();
Environment.Exit(4);
}
for (int i = 0; i < words.Count(); i++)
{
wordbank[words[i]] = weights[i];
}
Console.WriteLine("Sorting Dictionary...");
wordbank = wordbank.OrderByDescending(x => x.Value)
.ToDictionary(x => x.Key, x => x.Value);
Guess();
}
static void SaveWeights()
{
string data = "";
Console.WriteLine("Saving weights...");
foreach (int i in wordbank.Values)
{
data += i + "\n";
}
File.WriteAllText("weights.txt", data);
Console.WriteLine($"Current weights state saved to {Environment.CurrentDirectory}\\weights.txt");
data = "";
Console.WriteLine("Saving sorted data...");
foreach (string s in wordbank.Keys)
{
data += s + "\n";
}
File.WriteAllText("sortedinput.txt", data);
Console.WriteLine($"Current sorted wordbank state saved to {Environment.CurrentDirectory}\\sortedinput.txt");
}
static string printLine(int length, int scorelength)
{
string line = "=======";
for (int i = 0; i < length; i++)
{
line += "=";
}
line += "|====================|==============";
for (int i = 0; i < scorelength; i++)
{
line += "=";
}
return line;
}
}
}