|
const words = (await Bun.file("words_alpha.txt").text()).split("\n"); |
|
|
|
const rows = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]; |
|
|
|
const checkWord = (word: string) => { |
|
const letters = word.split(""); |
|
|
|
let lastRow = rows.findIndex((row) => row.includes(letters[0]!)); |
|
|
|
for (const letter of letters.slice(1)) { |
|
const nextRow = rows.findIndex((row) => row.includes(letter)); |
|
|
|
if (lastRow !== nextRow) { |
|
return -1; |
|
} |
|
} |
|
|
|
return lastRow; |
|
}; |
|
|
|
let oneRowWords: [string, number, number][] = []; |
|
|
|
for (const word of words) { |
|
const row = checkWord(word.trim()); |
|
|
|
if (row !== -1) { |
|
oneRowWords.push([word.trim(), row, word.trim().length]); |
|
} |
|
} |
|
|
|
oneRowWords = oneRowWords.filter(([word]) => word.length > 0); |
|
|
|
await Bun.write( |
|
"one-row-words.txt", |
|
oneRowWords.map(([word, row]) => `${word}:${row}`).join("\n") |
|
); |
|
|
|
const first = oneRowWords.filter(([_, row]) => row === 0); |
|
const second = oneRowWords.filter(([_, row]) => row === 1); |
|
const third = oneRowWords.filter(([_, row]) => row === 2); |
|
|
|
await Bun.write("qwe.txt", first.map(([word]) => word).join("\n")); |
|
await Bun.write("asd.txt", second.map(([word]) => word).join("\n")); |
|
await Bun.write("zxc.txt", third.map(([word]) => word).join("\n")); |
|
|
|
const sorted = oneRowWords.toSorted(function (a, b) { |
|
return b[0].length - a[0].length; |
|
}); |
|
|
|
await Bun.write( |
|
"one-row-words.sorted.txt", |
|
sorted.map(([word, row]) => `${word}:${row}`).join("\n") |
|
); |