#!/bin/node const fs = require('fs'); fs.readFile('input.txt', (err, data) => { const passwords = data.toString().split('\n').filter(l => l.length > 0); let q1 = 0, q2 = 0; for (const [format, password] of passwords.map(line => line.split(':'))) { const [occurrences, letter] = format.split(' '); const [min, max] = occurrences.split('-').map(n => parseInt(n)); console.log(min, max, letter, password); // q1 let count = 0; for (let j = 0; j < password.length; j++) { if (password[j] === letter) { count++; } } if (count >= min && count <= max) q1++; // q2 if (password[min] === letter && password[max] !== letter || password[min] !== letter && password[max] === letter) q2++; } console.log(q1, q2); });