-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.js
40 lines (35 loc) · 1.08 KB
/
day6.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
const { readData } = require('./readfile');
const input = readData('day6Input.txt');
const groups = input.split(/\s\n/);
const sum = (acc, cur) => acc + cur;
// question 1
// const countPresent = group => [...new Set(group.replace(/\n/g, ''))].length;
// const total = groups.map(countPresent).reduce((acc, cur) => acc + cur, 0);
// console.log(total);
// question 2
const all = 'abcdefghijklmnopqrstuvwxyz';
const countAllPresent = group => {
const lines = group.split(/\n/g);
let count = 0;
for(let i = 0; i < all.length; i++) {
let allPresent = true;
for(let j = 0; j < lines.length; j++) {
if (!lines[j].includes(all[i])) {
allPresent = false;
break;
}
}
if (allPresent) {
count++;
}
}
lines.forEach(line => {
const sorted = line.split("").sort().join("")
// console.log(sorted);
})
// console.log(count);
return count;
}
const total = groups.map(countAllPresent).reduce(sum, 0);
// const total = countAllPresent(groups[0]);
console.log(total);