Skip to content

Instantly share code, notes, and snippets.

@waggertron
Created October 13, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waggertron/394a977de5384f9839a6b824a2597b14 to your computer and use it in GitHub Desktop.
Save waggertron/394a977de5384f9839a6b824a2597b14 to your computer and use it in GitHub Desktop.
// https://www.reddit.com/r/dailyprogrammer/comments/98ufvz/20180820_challenge_366_easy_word_funnel_1/
const fs = require('fs');
const { promisify } = require('util');
const readFilePromise = promisify(fs.readFile);
const makeWordSet = async file => new Set((await promisify(fs.readFile)(file, 'utf8')).split('\n'));
function funnel(word1, word2) {
for (let i = 0; i < word1.length; i += 1) {
if (word1.slice(0, i) + word1.slice(i + 1) === word2) {
return true;
}
}
return false;
}
async function bonus(word) {
const wordSet = await makeWordSet('enable-word-list.txt');
const included = new Set();
for (let i = 0; i < word.length; i += 1) {
const slice = word.slice(0, i) + word.slice(i + 1);
if (wordSet.has(slice)) {
included.add(slice);
}
}
return Array.from(included);
}
async function bonus2() {
const wordSet = await makeWordSet('enable-word-list.txt');
const has5 = new Set();
for (const word of wordSet) {
const included = new Set();
for (let i = 0; i < word.length; i += 1) {
const slice = word.slice(0, i) + word.slice(i + 1);
if (wordSet.has(slice)) {
included.add(slice);
}
}
if (included.size === 5) {
has5.add(word);
}
}
return Array.from(has5);
}
async function start() {
console.log(funnel('boats', 'boat'));
console.log(await bonus('boats'));
const startTime = Date.now();
const result = await bonus2();
console.log(result, result.length);
console.log(`bonus2: ${Date.now() - startTime} ms`);
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment