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

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