Skip to content

Instantly share code, notes, and snippets.

@bigomega
Last active April 15, 2016 14:18
Show Gist options
  • Select an option

  • Save bigomega/6f91e064017ee631a66eca10d100717f to your computer and use it in GitHub Desktop.

Select an option

Save bigomega/6f91e064017ee631a66eca10d100717f to your computer and use it in GitHub Desktop.

Revisions

  1. bigomega revised this gist Apr 15, 2016. 1 changed file with 31 additions and 4 deletions.
    35 changes: 31 additions & 4 deletions wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,41 @@
    'use strict'
    const fs = require('fs')

    // util function modified from github.com/bucaran/sget
    function readLineSync(message) {
    message = message || ''
    const win32 = () => 'win32' === process.platform
    const readSync = function(buffer) {
    var fd = win32() ? process.stdin.fd : fs.openSync('/dev/stdin', 'rs')
    var bytes = fs.readSync(fd, buffer, 0, buffer.length)
    if (!win32()) fs.closeSync(fd)
    return bytes
    }
    return (function(buffer) {
    try {
    process.stdout.write(message + ' ')
    return buffer.toString(null, 0, readSync(buffer))
    } catch (e) {
    throw e
    }
    }(new Buffer(256)))
    }

    // const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    const board = {
    length: 4,
    height: 4,
    }
    // // NOTE: For notation, axes are swapped. eg., 0, 2 is first row second letter
    const inp = ['ogwc', 'ntca', 'aiap', 'lces']
    // const inp = ['ogwc', 'ntca', 'aiap', 'lces']
    const inp = []
    console.log(`Enter the ${board.length}x${board.height} matrix`)
    inp[0] = readLineSync()
    inp[1] = readLineSync()
    inp[2] = readLineSync()
    inp[3] = readLineSync()

    const matrix = inp.map(str => str.split(''))
    const emptyMatrix = Array(board.height + 1).join('-').split('').map(i => Array(board.length + 1).join('-').split('').map(j => 0))

    @@ -66,7 +93,7 @@ function traverse(matrix, pos, flagMatrix, localObj, lvl) {
    return []
    }

    const flagMatrixCopy = flagMatrix.map(row => row.splice())
    const flagMatrixCopy = flagMatrix.map(row => row.slice())
    flagMatrixCopy[pos.i][pos.j] = 1
    let allWords = []
    if (localObj[letter]._word) {
    @@ -101,8 +128,8 @@ for (let i = 0; i < board.length; i++) {
    }
    // Finding unique
    allWords = allWords.filter((item, pos) => allWords.indexOf(item) == pos)
    allWords.sort((a, b) => a.length < b.length)
    allWords.sort((a, b) => a.length >= b.length)

    console.log(allWords.length, 'words')
    console.log('')
    console.log(allWords.slice(0, 10))
    console.log(allWords)
  2. bigomega revised this gist Apr 15, 2016. 2 changed files with 2 additions and 235888 deletions.
    4 changes: 2 additions & 2 deletions wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    'use strict'
    const fs = require('fs')

    const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    // const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    // const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    const board = {
    length: 4,
    height: 4,
    235,886 changes: 0 additions & 235,886 deletions words2.txt
    0 additions, 235,886 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  3. bigomega revised this gist Apr 15, 2016. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    'use strict'
    const fs = require('fs')

    // const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    // const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    const board = {
    length: 4,
    height: 4,
    @@ -90,13 +90,19 @@ function traverse(matrix, pos, flagMatrix, localObj, lvl) {
    return allWords
    }

    // const structuredCorpus = structureCorpus(corpus)
    const structuredCorpus = structureCorpus(['a', 'abs', 'absolute', 'allah', 'almighty', 'spec', 'ties', 'cap', 'taps', 'laces', 'alias'])
    const structuredCorpus = structureCorpus(corpus)
    // const structuredCorpus = structureCorpus(['a', 'abs', 'absolute', 'allah', 'almighty', 'spec', 'ties', 'cap', 'taps', 'laces', 'alias'])

    let allWords = []
    for (let i = 0; i < board.length; i++) {
    for (let j = 0; j < board.height; j++) {
    allWords = allWords.concat(traverse(matrix, { i, j }, emptyMatrix, structuredCorpus, 0))
    }
    }
    console.log(allWords)
    // Finding unique
    allWords = allWords.filter((item, pos) => allWords.indexOf(item) == pos)
    allWords.sort((a, b) => a.length < b.length)

    console.log(allWords.length, 'words')
    console.log('')
    console.log(allWords.slice(0, 10))
  4. bigomega revised this gist Apr 15, 2016. 1 changed file with 63 additions and 21 deletions.
    84 changes: 63 additions & 21 deletions wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,12 @@
    const fs = require('fs')

    // const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    const corpus = fs.readFileSync('/usr/share/dict/words', 'utf-8').split('\n').map(word => word.toLowerCase())
    const board = {
    length: 4,
    height: 4,
    }
    // // NOTE: For notation, axes are swapped. eg., 0, 2 is first row second letter
    const inp = ['ogwc', 'ntca', 'aiap', 'lces']
    const matrix = inp.map(str => str.split(''))
    const emptyMatrix = Array(board.height + 1).join('-').split('').map(i => Array(board.length + 1).join('-').split('').map(j => 0))
    @@ -15,46 +17,86 @@ const ALPHABETS = 'abcdefghijklmnopqrstuvwxyz'.split('')
    function place(obj, word) {
    let localObj = obj
    const letters = word.split('')
    for(let i = 0; i < letters.length; i++) {
    for (let i = 0; i < letters.length; i++) {
    const letter = letters[i]
    if(!localObj[letter])
    if (!localObj[letter]) {
    localObj[letter] = {}
    if(i === letters.length - 1)
    }
    if (i === letters.length - 1) {
    localObj[letter]._word = true
    }
    localObj = localObj[letter]
    }
    return obj
    }

    function structureCorpus(words) {
    let structured = {}
    for(let i = 0; i < words.length; i++) {
    // Using for loops instead of forEach for efficiency
    for (let i = 0; i < words.length; i++) {
    const word = words[i]
    if(!word)
    if (!word) {
    continue
    }
    structured = place(structured, word)
    // break
    }
    return structured
    }

    function traverse(matrix, flagMatrix, localObj) {
    // - fn:(matrix, pos, flagMatrix, localStruc)
    // - If flagged, return []
    // - create a flagMatrixCopy
    // - Flag value
    // - if in localStruc
    // - if _word, also send ["<letter>"]
    // - for every position around it
    // - get fn(matrix, newPos, flagMatrix, localStruc[letter])
    // - return with <letter> prepended to each value
    // - else return []
    // - For every value in matrix
    // - fn(matrix, value, flagMatrix, structured)

    }
    function traverse(matrix, pos, flagMatrix, localObj, lvl) {
    if (flagMatrix[pos.i][pos.j])
    return []

    // const structured = structureCorpus(corpus)
    const structured = structureCorpus(['a', 'abs', 'absolute', 'allah', 'almighty'])
    console.log(JSON.stringify(structured))
    const letter = matrix[pos.i][pos.j]
    if (!localObj[letter]) {
    return []
    }

    // - For every value in matrix
    // - fn:(matrix, pos, flagMatrix, localStruc)
    // - If flagged, return []
    // - create a flagMatrixCopy
    // - Flag value
    // - if in localStruc
    // - if _word, also send ["<letter>"]
    // - for every position around it
    // - return fn(matrix, flagMatrix, localStruc[letter]).map(partWord => letter + partWord)
    // - else return []
    const flagMatrixCopy = flagMatrix.map(row => row.splice())
    flagMatrixCopy[pos.i][pos.j] = 1
    let allWords = []
    if (localObj[letter]._word) {
    allWords.push(letter)
    }
    for (let i = -1; i <= 1; i++) {
    const newI = pos.i + i
    if (newI < 0 || newI >= board.length) {
    continue
    }
    for (let j = -1; j <= 1; j++) {
    const newJ = pos.j + j
    if (newJ >= 0 && newJ < board.height) {
    const newPos = { i: newI, j: newJ }
    const others = traverse(matrix, newPos, flagMatrixCopy, localObj[letter], lvl + 1).map(word => letter + word)
    allWords = allWords.concat(others)
    }
    }
    }

    // - fn(matrix, value, flagMatrix, structured)
    return allWords
    }

    // const structuredCorpus = structureCorpus(corpus)
    const structuredCorpus = structureCorpus(['a', 'abs', 'absolute', 'allah', 'almighty', 'spec', 'ties', 'cap', 'taps', 'laces', 'alias'])

    let allWords = []
    for (let i = 0; i < board.length; i++) {
    for (let j = 0; j < board.height; j++) {
    allWords = allWords.concat(traverse(matrix, { i, j }, emptyMatrix, structuredCorpus, 0))
    }
    }
    console.log(allWords)
  5. bigomega revised this gist Apr 15, 2016. 2 changed files with 235945 additions and 1 deletion.
    60 changes: 59 additions & 1 deletion wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,60 @@
    'use strict'
    const fs = require('fs')
    const fs = require('fs')

    // const corpus = fs.readFileSync('words2.txt', 'utf-8').split('\n').map(word => word.toLowerCase())
    const board = {
    length: 4,
    height: 4,
    }
    const inp = ['ogwc', 'ntca', 'aiap', 'lces']
    const matrix = inp.map(str => str.split(''))
    const emptyMatrix = Array(board.height + 1).join('-').split('').map(i => Array(board.length + 1).join('-').split('').map(j => 0))

    const ALPHABETS = 'abcdefghijklmnopqrstuvwxyz'.split('')

    function place(obj, word) {
    let localObj = obj
    const letters = word.split('')
    for(let i = 0; i < letters.length; i++) {
    const letter = letters[i]
    if(!localObj[letter])
    localObj[letter] = {}
    if(i === letters.length - 1)
    localObj[letter]._word = true
    localObj = localObj[letter]
    }
    return obj
    }

    function structureCorpus(words) {
    let structured = {}
    for(let i = 0; i < words.length; i++) {
    const word = words[i]
    if(!word)
    continue
    structured = place(structured, word)
    // break
    }
    return structured
    }

    function traverse(matrix, flagMatrix, localObj) {

    }

    // const structured = structureCorpus(corpus)
    const structured = structureCorpus(['a', 'abs', 'absolute', 'allah', 'almighty'])
    console.log(JSON.stringify(structured))

    // - For every value in matrix
    // - fn:(matrix, pos, flagMatrix, localStruc)
    // - If flagged, return []
    // - create a flagMatrixCopy
    // - Flag value
    // - if in localStruc
    // - if _word, also send ["<letter>"]
    // - for every position around it
    // - return fn(matrix, flagMatrix, localStruc[letter]).map(partWord => letter + partWord)
    // - else return []

    // - fn(matrix, value, flagMatrix, structured)
    235,886 changes: 235,886 additions & 0 deletions words2.txt
    235,886 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  6. bigomega created this gist Apr 15, 2016.
    2 changes: 2 additions & 0 deletions wordcrack.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    'use strict'
    const fs = require('fs')