Published
Edited
Dec 3, 2020
Insert cell
Insert cell
sledrunhits(testinput)([3,1]) // test part 1
Insert cell
sledrunhits(input)([3,1]) // part 1
Insert cell
slopesEval(testinput) // test part 2
Insert cell
slopesEval(input) // part 2
Insert cell
// String[][] -> Number
slopesEval = data => [[1,1],[3,1],[5,1],[7,1],[1,2]]
.map(slope => sledrunhits(data)(slope))
.reduce((acc,inc) => inc * acc, 1) // product of array
Insert cell
// String[][] -> [Number, Number] -> Number
sledrunhits = data => slope => sledrun(formatInput(data))(slope).hits
Insert cell
// String[][] -> [Number, Number] -> SledRun{Number, Number, Number, Number, Number}
sledrun = data => slope => {
let sled = {slopeX: slope[0], slopeY: slope[1], hits: 0, x: 0, y: 0}
while(sled.y < data.length - 1) {
sled.x = (sled.x + sled.slopeX) % data[0].length // wrap
sled.y += sled.slopeY
if(data[sled.y][sled.x]=="#") sled.hits++
}
return sled
}
Insert cell
// String -> String[][]
formatInput = x => x.split("\n").map(d => d.split(""))
Insert cell
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