Notebooks 2.0 is here.

Published
Edited
Dec 3, 2020
Insert cell
Insert cell
Insert cell
parseInput = i => i.split('\n').map(l => l.split(''))
Insert cell
testMap = parseInput(testInput)
Insert cell
getCell = (map, x, y) => {
const mapX = x % map[y].length;
return map[y][mapX];
}
Insert cell
getCell(testMap, 0, 0)
Insert cell
getCell(testMap, 3, 1)
Insert cell
getCell(testMap, 6, 2)
Insert cell
countTrees = (map, right, down) => {
let x = 0, y = 0, count = 0;
while (y < map.length) {
if ( getCell(map, x, y) === '#' ) count++;
x += right;
y += down;
}
return count;
}
Insert cell
countTrees(testMap, 3, 1)
Insert cell
Insert cell
Insert cell
countTrees( parseInput(input), 3, 1 )
Insert cell
Insert cell
part2 = ( map ) => countTrees( map, 1, 1 ) * countTrees( map, 3, 1 ) * countTrees( map, 5, 1 ) * countTrees( map, 7, 1 ) * countTrees( map, 1, 2 );
Insert cell
part2( testMap );
Insert cell
part2( parseInput( input ) );
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