forked from fredoverflow/wordle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordle4.java
More file actions
259 lines (231 loc) · 8.28 KB
/
Wordle4.java
File metadata and controls
259 lines (231 loc) · 8.28 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/*
Matt Parker
Can you find: five five-letter words with twenty-five unique letters?
FJORD
GUCKS
NYMPH
VIBEX
WALTZ
Q
Matt: 32 days
Benjamin: 15 minutes
Fred: 1 seconds
Gustaf: 1 seconds
Constraints:
- No duplicate letters (valid words have 5 unique characters)
- Order of letters irrelevant (ignore anagrams during search)
- i.e. Word is Set of Characters
Representation:
- 32-bit number
- 26 bits for the letters A-Z
- 6 bits unused
25 0
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1 1 1 1 1 fjord
---D-F---J----O--R-------- fjord
--C---G---K-------S-U----- gucks
-------------------------- fjord AND gucks (INTERSECTION)
--CD-FG--JK---O--RS-U----- fjord OR gucks (UNION)
*/
class Wordle4 {
private static List<String> rawWords;
public static void main(String[] args) throws IOException {
Stopwatch stopwatch = new Stopwatch();
rawWords = new ArrayList<>();
// Uncomment the following 1 line to find 538 solutions:
// rawWords.addAll(Files.readAllLines(Path.of("words_alpha.txt")));
// Uncomment the following 2 lines to find 10 solutions:
rawWords.addAll(Files.readAllLines(Path.of("wordle-nyt-answers-alphabetical.txt")));
rawWords.addAll(Files.readAllLines(Path.of("wordle-nyt-allowed-guesses.txt")));
rawWords.removeIf(word -> word.length() != 5);
System.out.println(rawWords.size() + " raw words");
int[] cookedWords = rawWords.stream()
// A---E------L---P---------- apple
// --C-----I--L-----------XY- cylix
// --C-----I--L-----------XY- xylic
.mapToInt(Wordle4::encodeWord)
// remove words with duplicate characters
.filter(word -> Integer.bitCount(word) == 5)
.sorted()
// remove anagrams
.distinct()
.toArray();
System.out.println(cookedWords.length + " cooked words\n");
Trie root = new Trie();
root.stopwatch = stopwatch;
for (int word:cookedWords) {
Trie node = root;
for (int i = 0; i < 26; i++) {
if(((word >> i) & 1) > 0) {
node = node.addchild(1 << i);
}
}
}
root.search(0,-1,new int[5]);
System.out.println(stopwatch.elapsedTime());
}
private static String decodeWord(int word) {
// --C-----I--L-----------XY- cylix/xylic
return rawWords.stream()
.filter(raw -> encodeWord(raw) == word)
.collect(Collectors.joining("/", visualizeWord(word) + " ", ""));
}
public static String decodeWords(int... words) {
// ----E-----K-M--P---T------ kempt
// ---D---H------O------V---Z vozhd
// --C-----I--L-----------XY- cylix/xylic
// -B----G------N---R--U----- brung
// A----F----------Q-S---W--- waqfs
return Arrays.stream(words)
.mapToObj(Wordle4::decodeWord)
.collect(Collectors.joining("\n"));
}
private static int encodeWord(String raw) {
// 1 1 1 1 1 fjord
int bitset = 0;
for (int i = 0; i < raw.length(); ++i) {
bitset |= 1 << 26 >> raw.charAt(i);
}
return bitset;
}
private static String visualizeWord(int word) {
// 1 1 1 1 1
// ---D-F---J----O--R--------
char[] a = new char[26];
word <<= 6;
for (int i = 0; i < a.length; ++i, word <<= 1) {
a[i] = (word < 0) ? (char) ('A' + i) : '-';
}
return new String(a);
}
}
class Trie {
int bitmask;
Trie[] children;
Stopwatch stopwatch;
public Trie() {
bitmask = 0;
children = new Trie[26];
}
public Trie addchild(int ch) {
if((bitmask & ch) > 0) {
for (int i = 0; i < 26; i++) {
if(((ch >> i) & 1) > 0) {
return children[i];
}
}
} else {
bitmask |= ch;
for (int i = 0; i < 26; i++) {
if(((ch >> i) & 1) > 0) {
children[i] = new Trie();
return children[i];
}
}
}
//should not happen
System.out.println("ERROR");
return null;
}
public int getavailable(int used) {
return bitmask & (~used);
}
public void search(int used, int index, int... words) {
if(index < 4) {
findword(used, index+1, words);
} else {
System.out.printf("%s\n%s\n\n",
stopwatch.elapsedTime(),
Wordle4.decodeWords(words));
}
}
public void findword(int used, int index, int... words) {
int available1 = getavailable(used);
if(index > 0) {
int word = words[index-1];
for (int i = 0; i < 26; i++) {
if(((word >> i) & 1) > 0) {
int mask = (1 << i) -1;
available1 &= ~mask;
break;
}
}
}
if(available1 == 0) {
return;
}
int unused = 0;
for (int i = 0; i < 26; i++) {
if(((used >> i) & 1) == 0) {
unused++;
if(unused > 2) {
return;
}
}
if(((available1 >> i) & 1) > 0) {
Trie root2 = children[i];
int available2 = root2.getavailable(used);
if(available2 == 0) {
continue;
}
for (int j = i; j < 26; j++) {
if(((available2 >> j) & 1) > 0) {
Trie root3 = root2.children[j];
int available3 = root3.getavailable(used);
if(available3 == 0) {
continue;
}
for (int k = j; k < 26; k++) {
if(((available3 >> k) & 1) > 0) {
Trie root4 = root3.children[k];
int available4 = root4.getavailable(used);
if(available4 == 0) {
continue;
}
for (int l = k; l < 26; l++) {
if(((available4 >> l) & 1) > 0) {
Trie root5 = root4.children[l];
int available5 = root5.getavailable(used);
if(available5 == 0) {
continue;
}
for (int m = l; m < 26; m++) {
if(((available5 >> m) & 1) > 0) {
int wordmask = (1 << i) | (1 << j) | (1 << k) | (1 << l) | (1 << m);
used |= wordmask;
words[index] = wordmask;
search(used, index, words);
used ^= wordmask;
}
}
}
}
}
}
}
}
}
}
}
}
class Stopwatch {
private final Instant start = Instant.now();
public String elapsedTime() {
Instant now = Instant.now();
Duration duration = Duration.between(start, now).truncatedTo(ChronoUnit.MILLIS);
String formatted = DateTimeFormatter.ISO_LOCAL_TIME.format(duration.addTo(LocalTime.of(0, 0)));
return "[" + formatted + "] ";
}
}