Public
Edited
Dec 12, 2022
Insert cell
Insert cell
input_day12 = FileAttachment("input_day12.txt").text()
Insert cell
Insert cell
GraphologyLibrary = require('graphology-library@0.7.1/dist/graphology-library.min.js')
Insert cell
elevations=input.replace("S","a").replace("E","z").split("\n").map(r=>r.split("").map(l=>letter_map[l]))
Insert cell
nbr = elevations.length
Insert cell
nbc = elevations[0].length;
Insert cell
E = `${Math.floor(input.search("E")/(nbc+1))}-${input.search("E")%(nbc+1)}`
Insert cell
S = `${Math.floor(input.search("S")/(nbc+1))}-${input.search("S")%(nbc+1)}`
Insert cell
GraphologyLibrary.shortestPath.unweighted. bidirectional(graph,S,E).length-1
Insert cell
graph={
const graph = new graphology.Graph()
for(let row=0;row<nbr;row++){
for(let col=0;col<nbc;col++){
graph.addNode(`${row}-${col}`);
}
}
for(let row=0;row<nbr;row++){
for(let col=0;col<nbc;col++){
if(row>=1 && (elevations[row][col]-elevations[row-1][col])>=-1){
graph.addEdge(`${row}-${col}`,`${row-1}-${col}`);
}
if(row<=(nbr-2) && (elevations[row][col]-elevations[row+1][col])>=-1){
graph.addEdge(`${row}-${col}`,`${row+1}-${col}`);
}

if(col>=1 && (elevations[row][col]-elevations[row][col-1])>=-1){
graph.addEdge(`${row}-${col}`,`${row}-${col-1}`);
}
if(col<=(nbc-2) && (elevations[row][col]-elevations[row][col+1])>=-1){
graph.addEdge(`${row}-${col}`,`${row}-${col+1}`);
}
}
}
return graph
}
Insert cell
graph
Insert cell
letter_map={
let letter_map={}
"abcdefghijklmnopqrstuvwxyz".split("").forEach((elem, i) => {
letter_map[elem] = i
})
return letter_map
}
Insert cell
input=input_day12.slice(0,-1)
Insert cell
input_day12_test = FileAttachment("input_day12_test.txt").text()
Insert cell
Insert cell
graph2={
const graph = new graphology.Graph()
for(let row=0;row<nbr;row++){
for(let col=0;col<nbc;col++){
graph.addNode(`${row}-${col}`);
}
}
for(let row=0;row<nbr;row++){
for(let col=0;col<nbc;col++){
if(row>=1 && (elevations[row][col]-elevations[row-1][col])>=-1){
graph.addEdge(`${row-1}-${col}`,`${row}-${col}`);
}
if(row<=(nbr-2) && (elevations[row][col]-elevations[row+1][col])>=-1){
graph.addEdge(`${row+1}-${col}`,`${row}-${col}`);
}

if(col>=1 && (elevations[row][col]-elevations[row][col-1])>=-1){
graph.addEdge(`${row}-${col-1}`,`${row}-${col}`);
}

if(col<=(nbc-2) && (elevations[row][col]-elevations[row][col+1])>=-1){
graph.addEdge(`${row}-${col+1}`,`${row}-${col}`);
}
}
}
return graph
}
Insert cell
{
const paths = GraphologyLibrary.shortestPath.unweighted.singleSource(graph2,E)
const ijs = Object.keys(paths).map(k=> k.split("-").map(v=>+v))
const trek_lengths= ijs.filter( ij=> elevations[ij[0]][ij[1]]==0).map(ij=>paths[ij.join("-")].length-1)
return trek_lengths.sort((a,b)=>a-b)[0]
}
Insert cell
positions = GraphologyLibrary.layoutForceAtlas2()
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