-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
46 lines (38 loc) · 1.05 KB
/
mod.ts
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
import { loadWords } from "./dictionary.ts";
import {
inputToLetterPools,
wordLengthFromLetterPools,
wordMatchesLetterPools,
} from "./letter_pools.ts";
import { bold, green, red } from "std/fmt/colors.ts";
export function findWords(
searchStr: string,
dictionaryWords: string[],
debug = false,
) {
const pools = inputToLetterPools(searchStr);
if (debug) {
console.debug("Letter pools:", pools);
}
const poolWordLength = wordLengthFromLetterPools(pools);
const wordsMatchingLength = dictionaryWords.filter((word) =>
word.length === poolWordLength
);
const words = wordsMatchingLength.filter((word) =>
wordMatchesLetterPools(word, pools)
);
if (debug) {
console.log("Found words:", words);
}
return words;
}
const input = Deno.args[0];
if (input) {
const foundWords = findWords(input, loadWords());
if (foundWords.length > 0) {
console.log(bold("Found words:"));
console.log(foundWords.map((w, i) => ` ${i + 1}. ${green(w)}`).join("\n"));
} else {
console.log(red(bold("No words found!")));
}
}