-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.mjs
221 lines (187 loc) · 6.08 KB
/
solver.mjs
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
import { all, solutions, guesses } from "./words.mjs";
export { all, solutions, guesses };
// Utility constants
export const maskCount = 3 ** 5;
export const masks = new Array(maskCount).fill(null).map((_value, i) =>
i.toString(3).padStart(5, 0).split("")
);
Array.prototype.groupBy = function (groupingFunction = ident) {
return this.reduce(
(acc, curr) => {
const key = groupingFunction(curr)
if (acc.hasOwnProperty(key)) acc[key].push(curr)
else acc[key] = [curr]
return acc
},
{}
)
}
export const getMaskFromGuessAndSolution = (
guess,
solution,
) => {
const splitSolution = solution.split("");
const splitGuess = guess.split("");
let solutionCharsTaken = splitSolution.map((_value) => false);
let statuses = [];
// handle all correct cases first
splitGuess.forEach((letter, i) => {
if (letter === splitSolution[i]) {
statuses[i] = 2;
solutionCharsTaken[i] = true;
return;
}
});
splitGuess.forEach((letter, i) => {
if (statuses[i] || splitSolution[i] == letter) {
statuses[i] = 2;
return;
}
if (!splitSolution.includes(letter)) {
// handles the absent case
statuses[i] = 0;
return;
}
// now we are left with "present"s
const indexOfPresentChar = splitSolution.findIndex(
(x, index) => x === letter && !solutionCharsTaken[index],
);
if (indexOfPresentChar > -1) {
solutionCharsTaken[indexOfPresentChar] = true;
statuses[i] = 1;
return;
} else {
return;
}
});
return parseInt(statuses.join(""), 3);
};
export const convertMaskToFeedback = (maskID, guess) => {
const mask = masks[maskID];
const located = [".", ".", ".", ".", "."];
const unlocated = [[], [], [], [], []];
let has = {
addLetter: function (letter) {
if (this.letters[letter]) return;
else {
this.letters[letter] = {
min: this.letters[letter] = 0,
max: this.letters[letter] = 5,
};
}
},
incLetter: function (letter) {
if (!this.letters.hasOwnProperty(letter)) this.addLetter(letter);
this.letters[letter].min++;
},
limitLetter: function (letter) {
if (!this.letters.hasOwnProperty(letter)) this.addLetter(letter);
this.letters[letter].max = this.letters[letter].min;
},
letters: {},
};
const splitGuess = guess.split("");
// handle all correct cases first
splitGuess.forEach((letter, i) => {
if (mask[i] == 2) {
has.incLetter(letter);
located[i] = letter;
return;
}
});
splitGuess.forEach((letter, i) => {
// Skip letters in the correct place
if (mask[i] == 2) {
return;
}
// Set limits on letters there are no more of
if (mask[i] == 0) {
has.limitLetter(letter);
return;
}
if (mask[i] == 1) {
unlocated[i].push(letter);
has.incLetter(letter);
return;
}
});
return { located, unlocated, letters: has.letters };
};
export const getFeedbackFromGuessAndSolution = (guess, solution) =>
convertMaskToFeedback(getMaskFromGuessAndSolution(guess, solution), guess);
const hasLocated = (word, located) => word.match(new RegExp(located.join("")));
const hasUnlocated = (word, unlocated) =>
unlocated.every((letter, i) => word[i] != letter);
export const matchesFeedback = ({ located, unlocated, letters }) => (word) =>
hasLocated(word, located) && hasUnlocated(word, unlocated) &&
hasLetter(word, letters);
export const matchesMask = (mask, guess) => (solution) =>
matchesFeedback(convertMaskToFeedback(parseInt(mask, 3), guess))(solution);
export const filterWordsWithFeedback = (words, feedback) =>
words
.filter((word) => word.match(new RegExp(feedback.located.join(""))))
.filter((word) =>
feedback.unlocated.every((letter, i) => word[i] != letter)
)
.filter((word) =>
Object.entries(feedback.letters)
.every(
([hasLetter, range]) =>
word.split("").filter((wordLetter) => wordLetter == hasLetter)
.length >= range.min &&
word.split("").filter((wordLetter) => wordLetter == hasLetter)
.length <= range.max,
)
);
export const filterWordsWithMask = (words, guess, mask) =>
filterWordsWithFeedback(
words,
convertMaskToFeedback(mask, guess),
);
export const refineWordsForGuess = (words, guess, solution) =>
filterWordsWithFeedback(
words,
getFeedbackFromGuessAndSolution(guess, solution),
);
export const getQualityOfMaskForGuess = (solutions, maskID, guess) => {
const refinedSolutions = filterWordsWithMask(solutions, guess, maskID);
if (refinedSolutions.length == 0) {
return { guess, quality: 0 };
} else {
const probability = refinedSolutions.length / solutions.length;
const information = Math.log2(1 / probability);
return { guess, quality: probability * information };
}
};
export const getQualityOfGuess = (guess, solutions) => {
const quality = masks.map(
(_mask, maskID) => getQualityOfMaskForGuess(solutions, maskID, guess),
).reduce(
(acc, curr) => acc + curr.quality,
0,
);
return quality;
};
export const getBucket = (guess) => (solution) =>
getMaskFromGuessAndSolution(guess, solution);
const getProp = (propName) => (obj) => obj[propName];
const ident = (obj) => obj;
export const getBucketSizes = (guess, solutions = solutions) =>
Object.values(
solutions.map(getBucket(guess)).groupBy(ident),
).map(getProp("length"));
export const rateBuckets = (solutions = solutions, bucketSizes) => {
let quality = 0;
bucketSizes.forEach((bucketSize) => {
const probability = bucketSize / solutions.length;
const information = Math.log2(1 / probability);
quality += probability * information;
});
return quality;
};
export const rateGuess = (allowedSolutions = solutions) => (guess) =>
rateBuckets(allowedSolutions, getBucketSizes(guess, allowedSolutions)) +
(allowedSolutions.includes(guess) ? 0.001 : 0);
const getIndexOfLargest = (iMax, x, i, arr) => x > arr[iMax] ? i : iMax;
export const getBestGuess = (allowedSolutions = solutions) =>
all[all.map(rateGuess(allowedSolutions)).reduce(getIndexOfLargest, 0)];