Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active January 5, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghaiklor/4e156a6c91594d0119d7 to your computer and use it in GitHub Desktop.
Save ghaiklor/4e156a6c91594d0119d7 to your computer and use it in GitHub Desktop.
Advent of Code (Day 19 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8');
const REPLACEMENTS = INPUT.split('\n\n')[0].split('\n').reduce((map, r) => map.set(r.split(' => ')[1], r.split(' => ')[0]), new Map());
let MOLECULE = INPUT.split('\n\n')[1];
let count = 0;
while (MOLECULE !== 'e') {
const randomMolecule = Array.from(REPLACEMENTS.keys())[Math.round(Math.random() * REPLACEMENTS.size)];
MOLECULE = MOLECULE.replace(randomMolecule, match => {
count++;
return REPLACEMENTS.get(match);
});
console.log(`${MOLECULE} -> ${count}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment