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.
ES6 based solution for Advent Of Code, Day 19
'use strict'
var fs = require('fs')
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))
permutations.push(...linePermutations)
}
permutations = unique(permutations)
console.log(permutations.length)
// run with `node --harmony_destructuring 19.js`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment