Solve AOC day 9 pt 2

This commit is contained in:
2021-12-11 14:22:02 -07:00
parent 63e5ac2a4f
commit 5f6254cf0c
2 changed files with 23 additions and 3 deletions

View File

@@ -54,10 +54,17 @@
(dayTodo 8)
(day 9
(load "day9.kiss")
(assert (= 4 (count (lowPoints "src/year2021/inputs/day9-example.txt"))))
// (highlightLowPoints "src/year2021/inputs/day9.txt")
(print (apply + (for =>_ height (lowPoints "src/year2021/inputs/day9.txt") (+ 1 height))))
)
(let [exampleLowPoints (lowPoints "src/year2021/inputs/day9-example.txt")]
(assert (= 4 (count exampleLowPoints)))
(assert (= 9 (count (basinAround (.next (exampleLowPoints.keys)) "src/year2021/inputs/day9-example.txt")))))
(let [realLowPoints (lowPoints "src/year2021/inputs/day9.txt")
realBasins (for =>lp _ realLowPoints (basinAround lp "src/year2021/inputs/day9.txt"))
basinSizes (for b realBasins (count b))]
(assert (= 480 (apply + (for =>_ height realLowPoints (+ 1 height)))))
(assert (= 1045660 (apply * (.slice (reversed (sort basinSizes)) 0 3))))))
(dayTodo 10)
(dayTodo 11)
(dayTodo 12)

View File

@@ -47,6 +47,19 @@
(when isLow (dictSet lowPoints key height))))
lowPoints))
(function :Map<String,Int> basinAround [:String lowPoint file &opt :Map<String,Int> basinAlready :Map<String,Int> allPoints :Map<String,Int> pointsToCheck]
(set allPoints (or allPoints (pointMap file)))
(set pointsToCheck (or pointsToCheck (pointMap file)))
(set basinAlready (or basinAlready (new Map)))
(dictSet basinAlready lowPoint (dictGet allPoints lowPoint))
(pointsToCheck.remove lowPoint)
(doFor =>point _ (adjacentPointMap lowPoint allPoints)
(whenLet [(when (< h 9) h) (dictGet pointsToCheck point)]
(basinAround point file basinAlready allPoints pointsToCheck)))
basinAlready)
(function :Void highlightLowPoints [file]
(let [writer (Ansi.writer (Sys.stdout))
m (lowPoints file)