Published
Edited
Dec 2, 2021
Insert cell
# Advent of Code - Day 2
Insert cell
Insert cell
data = {
let x = 0
let y = 0
input
.split("\n")
.map(x => x.split(' '))
.forEach(v => {
if(v[0] == "forward") x += parseInt(v[1])
else if (v[0] == "down") y += parseInt(v[1])
else if (v[0] == "up") y -= parseInt(v[1])
})

return { x, y }
}
Insert cell
data.x * data.y
Insert cell
data2 = {
let aim = 0
let x = 0
let y = 0
input
.split("\n")
.map(x => x.split(' '))
.forEach(v => {
if(v[0] == "forward"){
x += parseInt(v[1])
y += aim * parseInt(v[1])
}
else if (v[0] == "down") aim += parseInt(v[1])
else if (v[0] == "up") aim -= parseInt(v[1])
})

return { x, y }
}
Insert cell
data2.x * data2.y
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