Created
July 21, 2024 23:41
-
-
Save scheibo/111e8dd127da9ef1a5733b4a0a42943b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const words = []; | |
for (const line of fs.readFileSync(process.argv[2], 'utf8').trim().split('\n')) { | |
const word = line.trim(); | |
words[word.length - 2] = words[word.length - 2] || []; | |
words[word.length - 2].push(word); | |
} | |
for (let n = 5; n <= 5; n++) { | |
const bucket = words[n - 2]; | |
const shapes = {}; | |
//console.error(i); | |
//const pairs = consecutive(bucket); | |
const result = findWordsWithTwoLetterDifference(bucket); | |
for (const [mask, all] of result.entries()) { | |
const all = result.get(mask); | |
if (all.length <= 1) continue; | |
const index = mask.indexOf('*'); | |
for (let i = 0; i < all.length; i++) { | |
for (let j = i + 1; j < all.length; j++) { | |
const key = `${'_'.repeat(index)}(${all[i].substring(index, index + 2)}|${all[j].substring(index, index + 2)})${'_'.repeat(n - index - 2)}`; | |
shapes[key] = shapes[key] || []; | |
shapes[key].push([all[i], all[j]]); | |
} | |
} | |
//console.log(mask, all.join(' ')); | |
} | |
const sorted = Object.keys(shapes); | |
sorted.sort((a, b) => shapes[b].length - shapes[a].length); | |
for (const key of sorted) { | |
console.log(key.toUpperCase(), shapes[key].map(pair => pair.join('/')).join(' ')); | |
} | |
} | |
function consecutive(words) { | |
if (words.length === 0) { | |
return {}; | |
} | |
const wordLength = words[0].length; | |
const result = {}; | |
for (let i = 0; i < words.length; i++) { | |
for (let j = i + 1; j < words.length; j++) { | |
for (let k = 0; k < wordLength - 1; k++) { | |
if (words[i].substring(k, k + 2) === words[j].substring(k, k + 2)) { | |
const key = `${'_'.repeat(k)}${words[i].substring(k, k + 2)}${'_'.repeat(words[i].length - k - 2)}`; | |
result[key] = result[key] || new Set(); | |
result[key].add(words[i]) | |
result[key].add(words[j]); | |
break; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
function findWordsWithTwoLetterDifference(words) { | |
const wordLength = words[0].length; | |
const maskedWordsMap = new Map(); | |
for (const word of words) { | |
if (word.endsWith('ed') || word.endsWith('s') || word.endsWith('er') || word.endsWith('ing')) continue; | |
for (let i = 0; i < wordLength - 1; i++) { | |
const maskedWord = word.substring(0, i) + "**" + word.substring(i + 2); | |
if (!maskedWordsMap.has(maskedWord)) { | |
maskedWordsMap.set(maskedWord, []); | |
} | |
maskedWordsMap.get(maskedWord).push(word); | |
} | |
} | |
return maskedWordsMap; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment