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 [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}`);