Published
Edited
Dec 9, 2020
Insert cell
md`# Advent of code 2020 - day 3`
Insert cell
answer2 = input2
.split('\n')
.map(el => [+el[6], +el[14]])
.map(el => slopeChecker(el[0], el[1]).filter(el => el).length)
.reduce((a, b) => a * b)
Insert cell
Insert cell
slopeChecker = (right, down) => {
if (down === 1)
// trees in the first row should not be counted, but we can see that there is no tree on data[0][0] so we skip error checking
return data.map((row, i) => row[i * right] === '#');
// if down is 2, filter out uneven rows from data
if (down === 2)
return data
.filter((_, i) => i % 2 === 0)
.map((row, i) => row[i * right] === '#');
}
Insert cell
answer1 = treeInRow.filter(el => el).length
Insert cell
treeInRow = data.map((row, i) => row[i * 3] === '#')
Insert cell
// split input and repeat each line 99 times (we're not sure how far the map goes to the right)
data = input.split('\n').map(el => el.repeat(99))
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