Published
Edited
Dec 2, 2021
Insert cell
# Advent of Code 2021 - Day 1
Insert cell
inputFile = FileAttachment("input.txt").text()
Insert cell
depths = inputFile.split('\n').map(Number)
Insert cell
## Puzzle 1
Insert cell
{
let increaseCount = 0
depths.forEach((depth, i) => {
let previous_index = i - 1
let previous_depth = depths[previous_index]
let current_depth = depth
if (current_depth > previous_depth) increaseCount += 1
})
return increaseCount
}
Insert cell
## Puzzle 2
Insert cell
{
let increaseRollingSumCount = 0
let previousSum = null

depths.forEach((depth, i) => {
if (i >= 2) {
let previousPreviousDepth = depths[i - 2]
let previousDepth = depths[i - 1]
let currentDepth = depth
let currentSum = depth + previousDepth + previousPreviousDepth

if (currentSum > previousSum & previousSum !== null) {
increaseRollingSumCount += 1
}
previousSum = currentSum
}
})
return increaseRollingSumCount
}
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