Published
Edited
Dec 22, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
addedInput.getMagnitude()
Insert cell
Insert cell
{
let maxMagnitude = 0
for (let idx1 = 0; idx1 < inputSnailNumbers.length; idx1++) {
for (let idx2 = 0; idx2 < inputSnailNumbers.length; idx2++) {
let nbr1 = new Tree(txtToObject(inputSnailNumbers[idx1].toString()))
let nbr2 = new Tree(txtToObject(inputSnailNumbers[idx2].toString()))
nbr1.add(nbr2)
let magnitude = nbr1.getMagnitude()
if (magnitude > maxMagnitude)
maxMagnitude = magnitude
}
}
return maxMagnitude
}
Insert cell
Insert cell
Insert cell
Insert cell
function txtToObject(txt) {
return JSON.parse(txt.replace(/\[/g, "{\"left\":")
.replace(/\]/g, "}")
.replace(/,/g, ", \"right\":"))
}
Insert cell
inputSnailNumbers = input.map( txt => new Tree(txtToObject(txt)))
Insert cell
test = processSnailNumber("[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]")
Insert cell
input = FileAttachment("18_input.txt").text().then( txt => txt.trim().split("\n"))
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