Published
Edited
Dec 13, 2020
Insert cell
Insert cell
Insert cell
// get the length of the set of unique question answers, and sum everything with reduce

answer1 = puzzleInput
.split('\n\n')
.map(el => [...new Set(el)].sort().filter(d => d !== '\n').length)
.reduce((a, b) => a + b)
Insert cell
Insert cell
puzzleInput // split on groups
.split('\n\n')
// split groups on new line to get persons
.map(group => group.split('\n'))
.findIndex(el => el.length === 1)
Insert cell
puzzleInput // split on groups
.split('\n\n')
// split groups on new line to get persons
.map(group => group.split('\n'))
Insert cell
answer2 = puzzleInput // split on groups
.split('\n\n')
// split groups on new line to get persons
.map(group => group.split('\n'))
// split answer characters
.map(group => group.map(answer => answer.split('')))
// for every character in persons 1's answers, check if they exist in the all of the other persons answers, get the length, sum the results. Only do this for groups with more than 1 person.
.map(group =>
group.length > 1
? group[0]
.map(question =>
group.slice(1).every(person => person.includes(question))
)
.filter(Boolean).length
: // for groups with 1 person, just count the answers
group[0].length
)
.reduce((a, b) => a + b)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more