Created
September 7, 2017 02:23
-
-
Save JohnathanWeisner/367c20d905f5ad9928e57010a75b63cb to your computer and use it in GitHub Desktop.
A script to search for all dictionary words on a stupid facebook post.
This file contains hidden or 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'); | |
let words = fs.readFileSync('/usr/share/dict/words', 'utf8') | |
.split('\n'); | |
let board = [ | |
'moronqisioteqwo', | |
'gdouchebagopzay', | |
'frnvfkttdnjddsk', | |
'eawtcolcaeoksru', | |
'lsiiwamchnwurcg', | |
'isddraecyipdjoa', | |
'cwfmfhtfucktard', | |
'iiilciawtlhwhlp', | |
'apgubgunasshole', | |
'seodxnuvffjrzxk', | |
'jdcgqcvdwufosct', | |
'rowpdickheadegd', | |
'idxvszkrxkpldaa', | |
'atngwhistledick' | |
]; | |
let wordsLookup = words.reduce((lookup, word) => { | |
if (word.length < 3) return lookup; | |
let currentLookup = lookup; | |
word.split('').forEach((char) => { | |
if (!currentLookup[char]) { | |
currentLookup[char] = {}; | |
} | |
currentLookup = currentLookup[char]; | |
}); | |
currentLookup[word] = word; | |
return lookup; | |
}, {}); | |
const directions = [ | |
{rowOffset:-1, colOffset:-1}, // up left | |
{rowOffset:-1, colOffset: 0}, // up | |
{rowOffset:-1, colOffset: 1}, // up right | |
{rowOffset: 0, colOffset: 1}, // right | |
{rowOffset: 1, colOffset: 1}, // down right | |
{rowOffset: 1, colOffset: 0}, // down | |
{rowOffset: 1, colOffset:-1} // down left | |
]; | |
board = board.map(row => row.split('')); | |
const boardHeight = board.length; | |
const boardWidth = board.length > 0 ? board[0].length : 0; | |
let allWordsFound = []; | |
const searchForWords = (rowIndex, colIndex, chars = '', lookup = wordsLookup, direction) => { | |
// Check board bounds | |
if (rowIndex < 0 || | |
colIndex < 0 || | |
rowIndex >= boardHeight || | |
colIndex >= boardWidth) return; | |
let char = board[rowIndex][colIndex]; | |
// Dead end. There are no words with the current characters. | |
if (!lookup[char]) return; | |
chars += char; | |
lookup = lookup[char]; | |
// Found a word! Yay! | |
if (lookup[chars] && chars.length > 1) { | |
allWordsFound.push(chars); | |
} | |
// If a direction is already specified then go that way! | |
if (direction) { | |
searchForWords( | |
rowIndex + direction.rowOffset, | |
colIndex + direction.colOffset, | |
chars, | |
lookup, | |
direction | |
); | |
} else { | |
directions.forEach((direction) => { | |
searchForWords( | |
rowIndex + direction.rowOffset, | |
colIndex + direction.colOffset, | |
chars, | |
lookup, | |
direction | |
); | |
}); | |
} | |
}; | |
board.forEach((row, rowIndex) => { | |
row.forEach((char, colIndex) => { | |
searchForWords(rowIndex, colIndex); | |
}); | |
}); | |
console.log(JSON.stringify(allWordsFound)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment