This commit is contained in:
nim-ka
2022-11-16 00:46:57 +00:00
parent 70bd11a687
commit 26120e2818
5 changed files with 125 additions and 14 deletions

15
2021/9.js Normal file
View File

@@ -0,0 +1,15 @@
function day9(input, part2) {
let grid = Grid.fromStr(input).num()
let lows = grid.findAllIndices((e, pt, g) => g.getAdjNeighbors(pt).every((nb) => e < g.get(nb)))
if (!part2) {
return lows.map((e) => grid.get(e) + 1).sum()
}
let sizes = lows.map((low) => grid.bfs(low, (e, pt, g) => {
return e == 9 || g.get(pt.path.last) < e ? Grid.BFS_STOP : Grid.BFS_CONTINUE
}).filter((e) => e.result == Grid.BFS_CONTINUE).length).sort((a, b) => b - a)
return sizes[0] * sizes[1] * sizes[2]
}