Published
Edited
Dec 7, 2020
Insert cell
Insert cell
Insert cell
testPeople = testInput.split('\n\n').map(l => l.split('\n'))
Insert cell
uniques = people => {
const uniqueAnswers = people.reduce((memo, answers) => {
answers.split('').forEach(answer => memo.add(answer));
return memo;
}, new Set());
return uniqueAnswers.size;
}
Insert cell
testPeople.map( uniques )
Insert cell
_ = require('lodash')
Insert cell
_.sum(testPeople.map( uniques ))
Insert cell
md`## Part 1`
Insert cell
Insert cell
_.sum( input
.split('\n\n')
.map(l => l.split('\n'))
.map(uniques) )
Insert cell
Insert cell
intersection = ( arr1, arr2 ) => {
if ( ! arr2 ) return arr1;
if ( ! arr1.filter ) throw arr1
return arr1.filter( code => arr2.includes( code ) )
}
Insert cell
intersection( ['a','b' ], ['a','c' ] )
Insert cell
intersection( ['a'] )
Insert cell
groupAnswers = people => people.reduce( intersection )
Insert cell
part2 = ( input ) => input
.split('\n\n')
.map(l => l.split('\n'))
.map(people => {
return people.map(p => p.split('')).reduce(intersection);
})
Insert cell
part2(testInput)
Insert cell
part2(testInput).map( arr => arr.length )
Insert cell
_.sum( part2(input).map( arr => arr.length ) )
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