Written by
on
on
Advent of Code Day2 Question 1
Advent of Code Day 2 Question 1 Answer
Advent of Code Day2 Question 1
The below code is done in javascript.
var fs = require("fs");
fs.readFile("./input.js", "utf8", (err, data) => {
if (err) {
throw err;
}
let twoCount = 0,
threeCount = 0;
data = data.split("\n");
for (let loop = 0; loop < data.length; loop++) {
let word = data[loop];
let obj = {};
let count3 = false,
count2 = false,
count2Word;
for (let loop2 = 0; loop2 < word.length; loop2++) {
obj[word[loop2]] =
typeof obj[word[loop2]] !== "undefined" ? obj[word[loop2]] + 1 : 1;
}
for (let a in obj) {
if (obj[a] === 2) {
count2 = true;
}
if (obj[a] === 3) {
count3 = true;
}
}
twoCount = twoCount + (count2 ? 1 : 0);
threeCount = threeCount + (count3 ? 1 : 0);
}
console.log(twoCount * threeCount);
});