Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 34.3 KB

06.md

File metadata and controls

71 lines (53 loc) · 34.3 KB

Day 05

Part 1

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)

Try it out on flems.io

group.split(/\n|/g)) is basically the same as group.split('\n').join('').split('').

Part 2

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)

flems

What did I learn?

Nothing because I opted to these lazy (non-exciting) solutions. But it's Sunday, so it's a-okay. :slightly_smiling_face: