const fs = require('fs'); Set.prototype.intersect = function(other) { let out = new Set(); other.forEach((entry) => { this.has(entry) && out.add(entry) }); return out; } fs.readFile('input', (err, data) => { if (err) throw err; let groups = data.toString().split("\n\n").map((group) => { let people = group.trim().split("\n").map((person) => new Set(person.trim().split(""))); return people.reduce((sum,person) => sum.intersect(person)); }); console.log( groups.reduce((sum, group) => sum + group.size, 0) ); });