AdventOfCode/JS/day6/solution_b.mjs
Tobias Berger 6c5733ca59
JavaScript Day 6
Signed-off-by: Tobias Berger <tobi.berger13@gmail.com>
2022-12-01 13:26:31 +01:00

31 lines
820 B
JavaScript

import fs from "fs";
/**
* Main function for Puzzle B
*
* @param {string} data - Puzzle input as a single string.
* @returns {number} Sum of amount of questions answered positively by everyone per group.
*/
async function main(data) {
const groups = data.split(/\r?\n\r?\n/).map((group) => group.split(/\r?\n/));
/** @type {number[]} */
const answer_count = [];
for (const group of groups) {
let answers = 0;
for (const char of "abcdefghijklmnopqrstuvwxyz") {
if (group.reduce((prev, cur) => prev && cur.includes(char), true)) {
answers++;
}
}
answer_count.push(answers);
}
return answer_count.reduce((prev, cur) => prev + cur, 0);
}
fs.readFile("input", (err, data) => {
if (err) throw err;
main(data.toString()).then(console.log).catch(console.error);
});