Created
October 13, 2018 23:09
-
-
Save waggertron/394a977de5384f9839a6b824a2597b14 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
// 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