Last active
January 5, 2016 16:43
-
-
Save ghaiklor/98ee17f44729e0222185 to your computer and use it in GitHub Desktop.
Advent of Code (Day 19 Part 1)
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
const fs = require('fs'); | |
const INPUT = fs.readFileSync('./input.txt', 'utf-8'); | |
const REPLACEMENTS = INPUT.split('\n\n')[0].split('\n'); | |
const MOLECULE = INPUT.split('\n\n')[1]; | |
const ALL_MOLECULES = new Set(); | |
REPLACEMENTS.forEach(replacement => { | |
const from = replacement.split(' => ')[0]; | |
const to = replacement.split(' => ')[1]; | |
const findRegex = new RegExp(from, 'g'); | |
const replaceRegex = new RegExp(from); | |
let match; | |
while (match = findRegex.exec(MOLECULE)) { | |
ALL_MOLECULES.add(MOLECULE.slice(0, match.index) + MOLECULE.slice(match.index).replace(replaceRegex, to)); | |
} | |
}); | |
console.log(ALL_MOLECULES.size); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment