Public
Edited
Nov 11, 2022
1 star
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split('\n')
.map(str => str.split('').map(c => (c == '#' ? 1 : 0)));
}
Insert cell
Insert cell
function tobogganRun(colStep, rowStep, trees) {
let collisions = 0;
let col = 0;

for (let row = 0; row < trees.length; row += rowStep) {
collisions += trees[row][col];
col = (col + colStep) % trees[0].length;
}
return collisions;
}
Insert cell
function part1(input) {
return tobogganRun(3, 1, parse(input));
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const trees = parse(input);
return (
tobogganRun(1, 1, trees) *
tobogganRun(3, 1, trees) *
tobogganRun(5, 1, trees) *
tobogganRun(7, 1, trees) *
tobogganRun(1, 2, trees)
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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