Skip to content

Instantly share code, notes, and snippets.

@MJGTwo
Last active December 4, 2019 18:31
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 MJGTwo/c43b2ed9bfec55b25e94b417ab15df20 to your computer and use it in GitHub Desktop.
Save MJGTwo/c43b2ed9bfec55b25e94b417ab15df20 to your computer and use it in GitHub Desktop.
aoc 2019 day 4 solution
const parse = text => text.split("-").map(bound => +bound);
const range = (upperBound, lowerBound = 0) =>
[...Array(upperBound - lowerBound).keys()]
.map(i => i + lowerBound)
.map(i => `${i}`);
const generate = bounds => range(bounds[1], bounds[0]);
const rule1 = /^0*1*2*3*4*5*6*7*8*9*$/; //number to the right is greater than or equal to previous number
const rule2 = /\d*(\d)\1\d*$/; //there is at least one duplicate neighbor
const rule3 = sequence =>
sequence
.split(/(?<=(.))(?!\1|$)/) //split into groups of duplicate numbers as well as single numbers
.reduce((exactlyTwo, el) => (el.length === 2 ? true : exactlyTwo), false);
const p1 = generate(parse(input))
.filter(rule1.test.bind(rule1))
.filter(rule2.test.bind(rule2));
const p2 = p1.filter(rule3);
console.log(p1.length);
console.log(p2.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment