31 lines
774 B
JavaScript
31 lines
774 B
JavaScript
const fs = require('fs');
|
|
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
|
|
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}`); |