Skip to content

Instantly share code, notes, and snippets.

@ideadapt
Last active April 13, 2016 15:23
Show Gist options
  • Save ideadapt/6b4896048062fb005506614615c26976 to your computer and use it in GitHub Desktop.
Save ideadapt/6b4896048062fb005506614615c26976 to your computer and use it in GitHub Desktop.

Revisions

  1. ideadapt revised this gist Apr 13, 2016. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions advent-of-code-19-solution-es6.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    'use strict'

    var fs = require('fs')
    var crypto = require('crypto')
    var inFile = '19.txt'

    var content = fs.readFileSync(inFile, {encoding: 'UTF-8'})
    @@ -33,9 +32,7 @@ for(var line of termLines){
    let indizes = findAllIndexOf(text, term)
    let linePermutations = indizes
    .map(produce.bind(null, text, term, product))
    .map((permutation) => {
    return crypto.createHash('md5').update(permutation).digest('hex')
    })

    permutations.push(...linePermutations)
    }
    permutations = unique(permutations)
  2. ideadapt created this gist Apr 13, 2016.
    45 changes: 45 additions & 0 deletions advent-of-code-19-solution-es6.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    'use strict'

    var fs = require('fs')
    var crypto = require('crypto')
    var inFile = '19.txt'

    var content = fs.readFileSync(inFile, {encoding: 'UTF-8'})
    var lines = content.split('\n')
    var text = lines[lines.length -1]
    var termLines = lines.slice(0,-1).filter(String)
    var permutations = []

    function findAllIndexOf(text, term){
    let match
    let matches = []
    let regexp = new RegExp(term, 'g')
    while ((match = regexp.exec(text)) != null) {
    matches.push(match.index)
    }
    return matches
    }

    function produce(text, term, product, index){
    return text.substr(0, index) + product + text.substr(index + term.length)
    }

    function unique(array){
    return [...new Set(array)]
    }

    for(var line of termLines){
    let [term, _, product] = line.split(' ')
    let indizes = findAllIndexOf(text, term)
    let linePermutations = indizes
    .map(produce.bind(null, text, term, product))
    .map((permutation) => {
    return crypto.createHash('md5').update(permutation).digest('hex')
    })
    permutations.push(...linePermutations)
    }
    permutations = unique(permutations)


    console.log(permutations.length)
    // run with `node --harmony_destructuring 19.js`