-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
147 lines (118 loc) · 3.73 KB
/
utils.js
File metadata and controls
147 lines (118 loc) · 3.73 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
const { ENGLISH_DOUBLE_LETTERS } = require("./constants");
const { getNLetterWordDict } = require("./dictionnary");
const decodeWith = (s, map) =>
s
.toLowerCase()
.split("")
.map((c) => (map[c] ? map[c] : c))
.join("");
const decypher = (s, map) =>
s
.toLowerCase()
.split("")
.map((c) => (map[c] ? map[c] : c))
.join("");
const doubleLetters = (word) =>
word.split("").filter((c, i, s) => i !== 0 && c == s[i - 1]);
const getConversionMap = (w1, w2) =>
w1.split("").reduce((acc, c, i) => ({ ...acc, [c]: w2.split("")[i] }), {});
const canBeAssigned = (word1, word2) => {
const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b);
const map = getConversionMap(word1, word2);
const reverseMap = Object.entries(map).reduce(
(acc, [k, v]) => ({ ...acc, [v]: k }),
{}
);
return equals(word1, decodeWith(word2, reverseMap));
};
const respectDoubleLetters = (map, cypher) =>
Object.entries(map)
.filter(([k, v]) => doubleLetters(cypher).includes(k))
.every(([k, v]) => ENGLISH_DOUBLE_LETTERS.includes(v));
const isUnique = (c, i, arr) => arr.indexOf(c) === i;
const noDupe = (map) => Object.values(map).every(isUnique);
const enrich = (maps, word, cypher) => {
console.log("enrich", word, maps.length);
return maps.reduce((acc, map, i) => {
if (i % 1000 == 0)
console.log(
"enrich",
`${i} / ${maps.length}`,
`result size: ${acc.length}`
);
const wordToFind = word;
const sameLengthWords = getNLetterWordDict(word.length);
const compatibleWithMap = (w, word, map) => {
const notAssignedOrValid = (char, i) =>
!map[char] || w.charAt(i) === map[char];
return word.split("").every(notAssignedOrValid);
};
const words = sameLengthWords
.filter((w) => compatibleWithMap(w, wordToFind, map))
.filter((w) => canBeAssigned(wordToFind, w));
if (!words.length) return acc;
const newMaps = words
.flatMap((word) => ({
...map,
...getConversionMap(wordToFind, word),
}))
.filter(noDupe)
.filter((map) => respectDoubleLetters(map, cypher));
return [...acc, ...newMaps];
}, []);
};
const getWordsByLength = (sentence) =>
sentence.split(" ").reduce(
(acc, w, i) => ({
...acc,
[w.length]: acc[w.length] ? [...acc[w.length], w].filter(isUnique) : [w],
}),
{}
);
const findBestNextWord = (words, map) => {
const uniqueLetters = (w) => w.split("").filter(isUnique);
const countUnkownChars = (w) =>
uniqueLetters(w).reduce(
(acc, c) => ({
...acc,
[w]: (acc[w] ? acc[w] : 0) + (map[c] ? 0 : 1),
}),
{}
);
const getWord = (obj) => Object.keys(obj)[0];
const getWordWithLeastUnkownRelative = (acc, w) => {
const getCount = (obj) => Object.values(obj)[0];
if (getWord(acc).length / getCount(acc) < getWord(w).length / getCount(w))
return w;
return acc;
};
const removeKownWords = (count) => Object.values(count)[0] !== 0;
const wordsLeft = words.map(countUnkownChars).filter(removeKownWords);
console.log(wordsLeft);
const oneLetterWords = wordsLeft.find((w) => getWord(w).length === 1);
if (oneLetterWords) return getWord(oneLetterWords);
const DEFAULT = { xxxxx: 1000 };
const diffCount = wordsLeft.reduce(getWordWithLeastUnkownRelative, DEFAULT);
return diffCount === DEFAULT ? null : getWord(diffCount);
};
const cleanUpSentence = (word) =>
word
.toLowerCase()
.replace(/'/gi, "")
.replace(/[^0-9a-z]/gi, " ")
.replace(/\s\s+/g, " ")
.trim();
module.exports = {
decodeWith,
decypher,
doubleLetters,
getConversionMap,
canBeAssigned,
respectDoubleLetters,
isUnique,
noDupe,
enrich,
getWordsByLength,
findBestNextWord,
cleanUpSentence,
};