AdventOfCode2020/day_2/task_2.js

26 lines
736 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 [firstPos, secondPos] = split;
// Check if the password is valid
if (!(pw.charAt(Number(firstPos) - 1) == wantedLetter ^ pw.charAt(Number(secondPos) - 1) == wantedLetter)) {
invalid.push(pw);
}
});
console.log(`Total: ${arr.length}`);
console.log(`Invalid: ${invalid.length}`);
console.log(`Valid: ${arr.length - invalid.length}`);