-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.js
92 lines (73 loc) · 2.01 KB
/
solver.js
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
(async () => {
console.log('Solving NYTimes Spelling Bee');
const submitBtn = document.getElementsByClassName('hive-action__submit')[0];
const chars = collectChars();
const solutions = await getListOfSolutions(chars);
for (let i = 0; i < solutions.length; i++) {
enterWord(solutions[i], chars, submitBtn);
}
})();
async function getListOfSolutions(chars) {
console.log('Loading word list from file');
const file = await loadWordList();
const words = file.split(/\r?\n/);
const solutions = [];
console.log('Checking word list for solutions');
words.forEach((word) => {
if (checkIfWordIsValid(word, chars)) {
solutions.push(word);
}
});
console.log('Found all solutions');
return solutions;
}
function checkIfWordIsValid(word, chars) {
if (word.length < 4) return false;
let hasCenterChar = false;
for (char of word) {
if (!chars[char]) return false;
if (chars[char].center) hasCenterChar = true;
}
return hasCenterChar;
}
function loadWordList() {
const wordList = fetch(
'https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt'
)
.then((res) => res.text())
.catch((err) => console.log(err));
return wordList;
}
function collectChars() {
const cells = document.getElementsByClassName('hive-cell');
const chars = {};
for (let i = 0; i < cells.length; i++) {
const char = cells[i].children[1].innerHTML;
const el = cells[i];
chars[char] = {
char: char,
center: i === 0,
ref: cells[i].children[0],
};
}
return chars;
}
function simulateMouseClick(element) {
const mouseClickEvents = ['mousedown', 'click', 'mouseup'];
mouseClickEvents.forEach((mouseEventType) =>
element.dispatchEvent(
new MouseEvent(mouseEventType, {
view: window,
bubbles: true,
cancelable: true,
buttons: 1,
})
)
);
}
function enterWord(word, chars, submitBtn) {
for (letter of word) {
simulateMouseClick(chars[letter].ref);
}
simulateMouseClick(submitBtn);
}