Solve AOC day 5 pt 2

This commit is contained in:
2021-12-07 12:07:54 -07:00
parent f577b9cc99
commit 0243e3140f
2 changed files with 15 additions and 8 deletions

View File

@@ -29,7 +29,9 @@
(day 5
(load "day5.kiss")
(assert (= 5 ~(numHotPositions "src/year2021/inputs/day5-example.txt")))
(print (numHotPositions "src/year2021/inputs/day5.txt")))
(assert (= 7473 (numHotPositions "src/year2021/inputs/day5.txt")))
(assert (= 12 (numHotPositions "src/year2021/inputs/day5-example.txt" true)))
(assert (= 24164 (numHotPositions "src/year2021/inputs/day5.txt" true))))
(day 6
(load "day6.kiss")
(assert (= "5934" (countAfter "src/year2021/inputs/day6-example.txt" 80)))

View File

@@ -6,30 +6,35 @@
(objectWith x1 y1 x2 y2))))
// An inclusive range whose start may be greater than its end
(function inclusiveRange [start end]
(function :Array<Int> inclusiveRange [start end]
(let [s (min start end)
e (max start end)]
(range s (+ e 1))))
(let [r (collect (range s (+ e 1)))]
(if (> start end)
(reversed r)
r))))
(function :Array<Point> pointsCoveredBy [:Line line]
(function :Array<Point> pointsCoveredBy [:Line line :Bool part2]
(cond
((= line.x1 line.x2)
(for y (inclusiveRange line.y1 line.y2) [line.x1 y]))
((= line.y1 line.y2)
(for x (inclusiveRange line.x1 line.x2) [x line.y1]))
// Diagonal lines:
(part2
(zipThrow (inclusiveRange line.x1 line.x2) (inclusiveRange line.y1 line.y2)))
(true
[])))
(function :Map<String,Int64> ventsByPos [file]
(function :Map<String,Int64> ventsByPos [file :Bool part2]
(let [lines (parseLines file)
:Map<String,Int64> theMap (new Map)]
(doFor line lines
(doFor point (pointsCoveredBy line)
(doFor point (pointsCoveredBy line part2)
(dictInc theMap (Std.string point) 1)))
theMap))
(function :Int numHotPositions [file]
(let [theMap (ventsByPos file)
(function :Int numHotPositions [file &opt :Bool part2]
(let [theMap (ventsByPos file ?part2)
two (Int64Helper.fromFloat 2)]
(count theMap ->vents #| vents >= two |#)))