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

32 lines
781 B
JavaScript

import fs from "fs";
/**
* Main function for Puzzle A
*
* @param {string} data - Puzzle input as a single string.
* @returns {number} Sum of amount of questions answered positively by anyone 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.includes(char)) {
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);
});