Skip to content

Commit 46ce4b0

Browse files
committed
wordle: find top words per letter position frequency
1 parent 8935e57 commit 46ce4b0

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed

README.md

+102-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
1-
# fun
1+
# Typescript playground
2+
3+
## Wordle
4+
5+
Letter Count:
6+
┬─────┬──────┐
7+
│ 0 │ 1 │
8+
┼─────┼──────┤
9+
│ 'a' │ 975 │
10+
│ 'b' │ 280 │
11+
│ 'c' │ 475 │
12+
│ 'd' │ 393 │
13+
│ 'e' │ 1230 │
14+
│ 'f' │ 229 │
15+
│ 'g' │ 310 │
16+
│ 'h' │ 387 │
17+
│ 'i' │ 670 │
18+
│ 'j' │ 27 │
19+
│ 'k' │ 210 │
20+
│ 'l' │ 716 │
21+
│ 'm' │ 316 │
22+
│ 'n' │ 573 │
23+
│ 'o' │ 753 │
24+
│ 'p' │ 365 │
25+
│ 'q' │ 29 │
26+
│ 'r' │ 897 │
27+
│ 's' │ 668 │
28+
│ 't' │ 729 │
29+
│ 'u' │ 466 │
30+
│ 'v' │ 152 │
31+
│ 'w' │ 194 │
32+
│ 'x' │ 37 │
33+
│ 'y' │ 424 │
34+
│ 'z' │ 40 │
35+
┴─────┴──────┘
36+
Letter Rank:
37+
┌─────────┬────────┬───────┐
38+
│ (index) │ Letter │ Count │
39+
├─────────┼────────┼───────┤
40+
│ 0 │ 'e' │ 1230 │
41+
│ 1 │ 'a' │ 975 │
42+
│ 2 │ 'r' │ 897 │
43+
│ 3 │ 'o' │ 753 │
44+
│ 4 │ 't' │ 729 │
45+
│ 5 │ 'l' │ 716 │
46+
│ 6 │ 'i' │ 670 │
47+
│ 7 │ 's' │ 668 │
48+
│ 8 │ 'n' │ 573 │
49+
│ 9 │ 'c' │ 475 │
50+
│ 10 │ 'u' │ 466 │
51+
│ 11 │ 'y' │ 424 │
52+
│ 12 │ 'd' │ 393 │
53+
│ 13 │ 'h' │ 387 │
54+
│ 14 │ 'p' │ 365 │
55+
│ 15 │ 'm' │ 316 │
56+
│ 16 │ 'g' │ 310 │
57+
│ 17 │ 'b' │ 280 │
58+
│ 18 │ 'f' │ 229 │
59+
│ 19 │ 'k' │ 210 │
60+
│ 20 │ 'w' │ 194 │
61+
│ 21 │ 'v' │ 152 │
62+
│ 22 │ 'z' │ 40 │
63+
│ 23 │ 'x' │ 37 │
64+
│ 24 │ 'q' │ 29 │
65+
│ 25 │ 'j' │ 27 │
66+
└─────────┴────────┴───────┘
67+
All words with the 7 top letters (and no duplicated letters):
68+
┌─────────┬─────────┐
69+
│ (index) │ Word │
70+
├─────────┼─────────┤
71+
│ 0 │ 'irate' │
72+
│ 1 │ 'later' │
73+
│ 2 │ 'trial' │
74+
│ 3 │ 'alter' │
75+
│ 4 │ 'alert' │
76+
│ 5 │ 'ratio' │
77+
│ 6 │ 'trail' │
78+
└─────────┴─────────┘
79+
80+
Top 3 letters per position
81+
┌─────────┬──────────────┬──────────────┬──────────────┐
82+
│ (index) │ 0 │ 1 │ 2 │
83+
├─────────┼──────────────┼──────────────┼──────────────┤
84+
│ 0 │ [ 's', 365 ][ 'c', 198 ][ 'b', 173 ]
85+
│ 1 │ [ 'a', 304 ][ 'o', 279 ][ 'r', 267 ]
86+
│ 2 │ [ 'a', 306 ][ 'i', 266 ][ 'o', 243 ]
87+
│ 3 │ [ 'e', 318 ][ 'n', 182 ][ 's', 171 ]
88+
│ 4 │ [ 'e', 422 ][ 'y', 364 ][ 't', 253 ]
89+
└─────────┴──────────────┴──────────────┴──────────────┘
90+
All words with the top 3 letters for each position (and no duplicated letters):
91+
┌─────────┬─────────┐
92+
│ (index) │ Word │
93+
├─────────┼─────────┤
94+
│ 0 │ 'coast' │
95+
│ 1 │ 'brine' │
96+
│ 2 │ 'saint' │
97+
│ 3 │ 'briny' │
98+
│ 4 │ 'crane' │
99+
│ 5 │ 'boast' │
100+
│ 6 │ 'crony' │
101+
│ 7 │ 'crone' │
102+
└─────────┴─────────┘

dist/index.cjs

+28
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,34 @@ var topWords = wordlist.filter((word) => {
23382338
});
23392339
console.log("All words with the 7 top letters (and no duplicated letters):");
23402340
console.table(topWords.map((word) => ({ Word: word })));
2341+
var countByIndex = wordlist.reduce((all, curr) => {
2342+
curr.split("").forEach((letter, index) => {
2343+
all[index] = all[index] || {};
2344+
all[index][letter] = (all[index][letter] || 0) + 1;
2345+
});
2346+
return all;
2347+
}, {});
2348+
var countByIndexArr = Object.entries(countByIndex);
2349+
console.log("");
2350+
countByIndexArr.forEach((arr) => {
2351+
console.log(Object.entries(arr[1]).sort((a, b) => a[0].localeCompare(b[0])).map((letter) => ({
2352+
[letter[0]]: letter[1]
2353+
})));
2354+
});
2355+
var countByIndexRank = countByIndexArr.map((position) => {
2356+
return Object.entries(position[1]).sort((a, b) => b[1] - a[1]).slice(0, 3);
2357+
});
2358+
console.log("Top 3 letters per position");
2359+
console.table(countByIndexRank);
2360+
var topWordsByIndex = wordlist.filter((word) => {
2361+
return word.split("").every((c, i) => {
2362+
return countByIndexRank[i].some(([topLetter]) => {
2363+
return topLetter === c;
2364+
});
2365+
}) && (/* @__PURE__ */ new Set([...word.split("")])).size === 5;
2366+
});
2367+
console.log("All words with the top 3 letters for each position (and no duplicated letters):");
2368+
console.table(topWordsByIndex.map((word) => ({ Word: word })));
23412369
var wordle = "wordle";
23422370

23432371
// src/index.ts

src/wordle/wordle.ts

+48
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,52 @@ const topWords = wordlist.filter((word) => {
4444
console.log("All words with the 7 top letters (and no duplicated letters):")
4545
console.table(topWords.map((word) => ({ Word: word })))
4646

47+
// count letters per index position
48+
const countByIndex = wordlist.reduce(
49+
(all: Record<number, LetterCount>, curr) => {
50+
curr.split("").forEach((letter, index) => {
51+
all[index] = all[index] || {}
52+
all[index][letter] = (all[index][letter] || 0) + 1
53+
})
54+
return all
55+
},
56+
{}
57+
)
58+
const countByIndexArr = Object.entries(countByIndex)
59+
console.log("")
60+
61+
countByIndexArr.forEach((arr) => {
62+
console.log(
63+
Object.entries(arr[1])
64+
.sort((a, b) => a[0].localeCompare(b[0]))
65+
.map((letter) => ({
66+
[letter[0]]: letter[1],
67+
}))
68+
)
69+
})
70+
71+
const countByIndexRank = countByIndexArr.map((position) => {
72+
return Object.entries(position[1])
73+
.sort((a, b) => b[1] - a[1])
74+
.slice(0, 3)
75+
})
76+
console.log("Top 3 letters per position")
77+
console.table(countByIndexRank)
78+
79+
// filter list to only words that have letters in the top letters for each index
80+
const topWordsByIndex = wordlist.filter((word) => {
81+
return (
82+
word.split("").every((c, i) => {
83+
return countByIndexRank[i].some(([topLetter]) => {
84+
return topLetter === c
85+
})
86+
}) && new Set([...word.split("")]).size === 5
87+
)
88+
})
89+
console.log(
90+
"All words with the top 3 letters for each position (and no duplicated letters):"
91+
)
92+
console.table(topWordsByIndex.map((word) => ({ Word: word })))
93+
// const commonWords = topWordsByIndex.filter((word) => topWords.includes(word))
94+
// console.table(commonWords.map((word) => ({ Word: word })))
4795
export const wordle = "wordle"

0 commit comments

Comments
 (0)