Skip to content

Instantly share code, notes, and snippets.

@devill
Last active February 5, 2022 15:41
Show Gist options
  • Select an option

  • Save devill/dc21a24dd425f6d7eef3f3edd6c6603e to your computer and use it in GitHub Desktop.

Select an option

Save devill/dc21a24dd425f6d7eef3f3edd6c6603e to your computer and use it in GitHub Desktop.
import { promises as fs } from 'fs';
Array.prototype.countBy = function () {
return this.reduce((acc, item) => {
acc[item] = acc[item] || 0;
acc[item] += 1
return acc;
}, {})
}
Array.prototype.sum = function () { return this.reduce((s,v) => s + v); }
Array.prototype.cache = function (callback) {
return this.reduce((acc,item) => {
acc[item] = callback(item);
return acc;
}, {});
}
let rawData = (await fs.readFile('./words.txt', 'utf8')).split('\n');
const charFrequencies = rawData.flatMap(w => w.split("")).countBy();
const positionFrequencies = Array(5).fill(0).map((_, pos) => {
return rawData.map(w => w[pos]).countBy();
});
let values = rawData.cache(w => w.split('').map((c,i) => charFrequencies[c] + 4 * positionFrequencies[i][c]).sum());
const duplicateLetters = (w1,w2) => 10 - [...new Set((w1+w2).split(''))].length;
rawData.forEach(w1 => rawData.forEach(w2 => {
console.log(values[w1] + values[w2] - duplicateLetters(w1,w2) * 10000, w1, w2);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment