-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 922 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const fs = require('fs');
(async () => {
const rawInputs = await fetchInputs();
const parsed = toArray(rawInputs);
const res = calculateSumOfTheCounts(parsed);
console.log(res);
})()
function fetchInputs() {
return new Promise((resolve, reject) => {
fs.readFile('./input', 'utf-8', (err, data) => {
if (err) return reject(err);
resolve(data);
})
})
}
const toArray = (input) => {
const arr = input.split(/\n\s*\n/);
return arr.map(a => a.replace(/\n/g, ' ').split(' '))
}
const calculateSumOfTheCounts = (answers) => {
return answers
.map(answer => parseAnswer(answer))
.reduce((oldVal, newVal) => oldVal + newVal);
}
const parseAnswer = (answer) => {
const split = answer.map(i => i.split(''))
const flat = split.flat();
// remove dublicates
const res = flat
.filter((answer, indx) =>
flat.indexOf(answer) === indx
);
return res.length;
}