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

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