AdventOfCode2020/day_2/task_1.js

31 lines
774 B
JavaScript
Raw Permalink Normal View History

2020-12-02 16:29:13 +00:00
const fs = require('fs');
2020-12-05 10:35:52 +00:00
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
2020-12-02 16:29:13 +00:00
const arr = file.split('\n');
let invalid = []
arr.forEach(item => {
// Split the puzzle input
let split = item.split(": ");
let pw = split[1];
split = split[0].split(" ");
let wantedLetter = split[1];
split = split[0].split("-");
let [lowerNum, upperNum] = split;
// Check if the password is valid
let count = 0;
for (let letter of pw) {
if (letter == wantedLetter) count++;
}
if (count < lowerNum || count > upperNum) {
invalid.push(pw);
}
});
console.log(`Total: ${arr.length}`);
console.log(`Invalid: ${invalid.length}`);
console.log(`Valid: ${arr.length - invalid.length}`);