Skip to content

Instantly share code, notes, and snippets.

@lizacodes
Last active December 5, 2020 08:42
Show Gist options
  • Select an option

  • Save lizacodes/91a7cbf1b3862380eb8c07f67e97e5aa to your computer and use it in GitHub Desktop.

Select an option

Save lizacodes/91a7cbf1b3862380eb8c07f67e97e5aa to your computer and use it in GitHub Desktop.
Advent of Code - Day 2
const fs = require('fs').promises;
const isPasswordValid = (password, policy) => {
const policyBreakdown = /(\d+)-(\d+) ([a-z])/ig;
const [, min, max, letter] = policyBreakdown.exec(policy)
if (password[min-1] === letter && password[max-1] === letter) {
return false;
}
return password[min-1] === letter || password[max-1] === letter
}
const solution = (inputs) => {
return inputs.split('\n').map((input) => {
const [policy, password] = input.split(': ');
return isPasswordValid(password, policy)
}).filter(isValid => isValid).length
}
(async () => {
const inputs = await fs.readFile('./inputs.txt')
console.log(solution(inputs.toString()))
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment