Published
Edited
Dec 18, 2020
Insert cell
Insert cell
input = (await FileAttachment("Avent of Code 2020 - Day 03.txt").text())
.trim()
.split("\n")
.map(line => mapstr(line, c => (c === "#" ? 1 : 0)))
Insert cell
function mapstr(str, callback) {
const arr = [];
for (let i = 0; i < str.length; i++) {
arr.push(callback(str[i]));
}
return arr;
}
Insert cell
answer_1 = {
const width = input[0].length;
let x = 0;
let trees = 0;
for (let y = 0; y < input.length; y++) {
trees += input[y][x];
x = (x + 3) % width;
}
return trees;
}
Insert cell
answer_2 = {
// const input = example_input;
const width = input[0].length;
let encounters = [];
let stepsx = [1, 3, 5, 7];
for (const step of stepsx) {
let trees = 0,
x = 0;
for (let y = 0; y < input.length; y++) {
trees += input[y][x];
x = (x + step) % width;
}
encounters.push(trees);
}
{
let trees = 0,
x = 0;
for (let y = 0; y < input.length; y += 2) {
trees += input[y][x];
x = (x + 1) % width;
}
encounters.push(trees);
}
// return encounters;
let mult = 1;
for (const trees of encounters) mult *= trees;
return { mult, encounters };
}
Insert cell
Insert cell
example_input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`
.trim()
.split("\n")
.map(line => mapstr(line, c => (c === "#" ? 1 : 0)))
Insert cell
density = {
let obstacles = 0;
let locations = 0;
for (const line of input) {
for (const v of line) {
obstacles += v;
locations++;
}
}
return { locations, obstacles, density: obstacles / locations };
}
Insert cell
(input.length * density.density) ** 5
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more