Calculate how many questions has one or more "yes" answer in each group. Calculate the sum of that result.
const input = `abc
a
b
c
ab
ac
a
a
a
a
b`
const result = input
.split('\n\n')
.map((group) => group.split(/\n|/g))
.map((answers) => answers.reduce((set, answer) => set.add(answer), new Set()))
.reduce((sum, set) => sum + set.size, 0)
console.log(result)
group.split(/\n|/g))
is basically the same as
group.split('\n').join('').split('')
.
Same as Part 1, but each question must have a "yes" answer from everyone in the group.
const result = input
.split('\n\n')
.map((group) => {
const people = group.split('\n')
const answers = people.join('').split('')
const answersMap = answers.reduce(
(map, answer) => map.set(answer, (map.get(answer) || 0) + 1),
new Map()
)
return [...answersMap].filter(
([question, count]) => count === people.length
)
})
.reduce((sum, arr) => sum + arr.length, 0)
console.log(result)
Nothing because I opted to these lazy (non-exciting) solutions. But it's Sunday, so it's a-okay. :slightly_smiling_face: