Published
Edited
Dec 6, 2020
Insert cell
Insert cell
part1(input) // part 1
Insert cell
part2(input) // part 2
Insert cell
part1 = str =>
str
.split("\n\n")
.map(group => group.replaceAll("\n",""))
.map(group => new Set(group))
.map(group => group.size)
.reduce((acc, inc) => inc + acc, 0)
Insert cell
part2(exampleinput)
Insert cell
part2 = str => {
const sets = str
.split("\n\n")
.map(group => group.replaceAll("\n",""))
.map(group => new Set(group))
const groups = str
.split("\n\n")
.map(group => group.split("\n"))
let answers = [];
for(let i = 0; i < groups.length; i++) {
let as = []
sets[i].forEach(answer => {
let all = true;
for(let j = 0; j < groups[i].length; j++) {
if(!groups[i][j].match(answer)) all = false;
}
if(all) as.push(answer)
})
answers.push(as);
}
return answers
.map(arr => arr.length)
.reduce((acc, inc) => acc + inc, 0)
}
Insert cell
exampleinput
.split("\n\n")
.map(group => group.replaceAll("\n",""))
.map(group => new Set(group))
Insert cell
Insert cell
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