Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:43
Show Gist options
  • Save ghaiklor/98ee17f44729e0222185 to your computer and use it in GitHub Desktop.
Save ghaiklor/98ee17f44729e0222185 to your computer and use it in GitHub Desktop.
Advent of Code (Day 19 Part 1)
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