Published
Edited
Dec 10, 2021
2 stars
Insert cell
# Advent of Code - Day 9
Insert cell
toMatrix = (input) => {
let m = input.split("\n").map(l => Array.from(l).map(x => parseInt(x)))
return m
}
Insert cell
test = toMatrix(`2199943210
3987894921
9856789892
8767896789
9899965678`)
Insert cell
findNeighboors = (x,y,m)=>{
const w = m[0].length
const h = m.length
let n = []
let xs = [-1,+1].filter(ox => x+ox >= 0 && x+ox < w)
let ys = [-1,+1].filter(oy => y+oy >= 0 && y+oy < h)

xs.forEach(ox => n.push({y, x: ox+x}))
ys.forEach(oy => n.push({y: y+oy, x}))

return n
}
Insert cell
findLowests = (m)=>{
const w = m[0].length
const h = m.length
let lowests = []

for (let x = 0; x < w; x++){
for (let y = 0; y < h; y++ ){
let n = findNeighboors(x,y,m)
if (m[y][x] < d3.min(n.map(pos => m[pos.y][pos.x]))) lowests.push({x,y})
}
}

return {w,h,lowests}
}
Insert cell
findLowests(test)
.lowests
.map(pos => test[pos.y][pos.x]+1)
.reduce((p,c)=>p+c)
Insert cell
Insert cell
findLowests(input)
.lowests
.map(pos => input[pos.y][pos.x]+1)
.reduce((p,c)=>p+c)
Insert cell
test[0][7]
Insert cell
findLowests(test)
.lowests
.map(pos => {
let positions = [{y:pos.y, x:pos.x}]

let candidates = positions.slice()
let hs = [test[pos.y][pos.x]]

while(candidates.length > 0){
const current = candidates.shift()

let ns = findNeighboors(current.x, current.y, test)
// .filter(candidate => !positions.every(p => p.x != candidate.x && p.y != candidate.y))
.filter(candidate => test[candidate.y][candidate.x] != 9)
.filter(candidate => test[candidate.y][candidate.x] > test[current.y][current.x])

candidates = candidates.concat(ns)
positions = positions.concat(ns)
ns.forEach(p => hs.push([p.y, p.x, test[p.y][p.x]]))
}

positions = [...new Set(positions.map(p => JSON.stringify(p)))].map(p => JSON.parse(p))
return {...pos, positions}
})
.sort((b,a) => a.positions.length - b.positions.length)
.slice(0,3)
.map(b => b.positions.length)
.reduce((p,c)=> p*c)
Insert cell
findLowests(input)
.lowests
.map(pos => {
let positions = [{y:pos.y, x:pos.x}]

let candidates = positions.slice()
let hs = [input[pos.y][pos.x]]

while(candidates.length > 0){
const current = candidates.shift()

let ns = findNeighboors(current.x, current.y, input)
.filter(candidate => !positions.every(p => p.x != candidate.x && p.y != candidate.y))
.filter(candidate => input[candidate.y][candidate.x] != 9)
.filter(candidate => input[candidate.y][candidate.x] > input[current.y][current.x])

candidates = candidates.concat(ns)
positions = positions.concat(ns)
ns.forEach(p => hs.push([p.y, p.x, input[p.y][p.x]]))
}

positions = [...new Set(positions.map(p => JSON.stringify(p)))].map(p => JSON.parse(p))
return {...pos, positions, hs}
})
.sort((b,a) => a.positions.length - b.positions.length)
.slice(0,3)
.map(b => b.positions.length)
.reduce((p,c)=> p*c)
Insert cell
lowests_input = findLowests(input)
.lowests
.map(pos => {
let positions = [{y:pos.y, x:pos.x}]

let candidates = positions.slice()
let hs = [input[pos.y][pos.x]]

while(candidates.length > 0){
const current = candidates.shift()

let ns = findNeighboors(current.x, current.y, input)
.filter(candidate => !positions.every(p => p.x != candidate.x && p.y != candidate.y))
.filter(candidate => input[candidate.y][candidate.x] != 9)
//.filter(candidate => input[candidate.y][candidate.x] == input[current.y][current.x] +1)
.filter(candidate => input[candidate.y][candidate.x] > input[current.y][current.x])

candidates = candidates.concat(ns)
positions = positions.concat(ns)
ns.forEach(p => hs.push([p.y, p.x, input[p.y][p.x]]))
}

positions = [...new Set(positions.map(p => JSON.stringify(p)))].map(p => JSON.parse(p))
return {...pos, positions, hs}
})
.sort((b,a) => a.positions.length - b.positions.length)
Insert cell
test.map((xs, y) => xs.map((v,x)=> [y,x,v])).flat()
Insert cell
lowests_input.map(b => b.positions).flat().map(p => [p.y, p.x])
//.map((xs, y) => xs.map((v,x)=> [y,x,v])).flat()
Insert cell
Plot.plot({
height: 1000,
width: 1000,
marks:[
Plot.cell(input.map((xs, y) => xs.map((v,x)=> [y,x,v])).flat(),
{
x: d => d[1],
y: d => d[0],
fill: d=> d[2]
}),
Plot.text(input.map((xs, y) => xs.map((v,x)=> [y,x,v])).flat(),
{
x: d => d[1],
y: d => d[0],
text: d=> d[2]
})
]
})
Insert cell
Plot.plot({
height: 1000,
width: 1000,
marks:[
Plot.cell(lowests_input.slice(0,3).map(b => b.positions).flat().map(p => [p.y, p.x]),
{
x: d => d[1],
y: d => d[0],
fill: d=> input[d[0]][d[1]]
}),
Plot.text(input.map((xs, y) => xs.map((v,x)=> [y,x,v])).flat(),
{
x: d => d[1],
y: d => d[0],
text: d=> d[2]
})
]
})
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