Published
Edited
Dec 1, 2020
Insert cell
Insert cell
solutionizer(sumScanPair) // part 1
Insert cell
solutionizer(sumScanTrip) // part 2
Insert cell
solutionizer = thingDoer(input)
Insert cell
thingDoer = data => func =>
func(data.split("\n").map(Number), 2020)
.reduce((acc,inc) => acc * inc, 1) // product of array
Insert cell
sumScanPair = function (arr, sum) {
for(let i = 0; i < arr.length - 1; i++) {
for(let j = i + 1; j < arr.length; j++) {
if(arr[i] + arr[j]==sum) return [arr[i],arr[j]]
}
}
}
Insert cell
sumScanTrip = function(arr, sum) {
for(let i = 0; i < arr.length - 2; i++) {
for(let j = i + 1; j < arr.length - 1; j++) {
for(let k = j; k < arr.length; k++) {
if(arr[i] + arr[j] + arr[k]==sum) return [arr[i],arr[j],arr[k]]
}
}
}
}
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