Last active
December 5, 2020 08:42
-
-
Save lizacodes/91a7cbf1b3862380eb8c07f67e97e5aa to your computer and use it in GitHub Desktop.
Advent of Code - Day 2
This file contains hidden or 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').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