diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index 61688d85..f268dea2 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -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))) diff --git a/projects/aoc/src/year2021/day5.kiss b/projects/aoc/src/year2021/day5.kiss index 9b8f7747..958d75dd 100644 --- a/projects/aoc/src/year2021/day5.kiss +++ b/projects/aoc/src/year2021/day5.kiss @@ -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 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 pointsCoveredBy [:Line line] +(function :Array 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 ventsByPos [file] +(function :Map ventsByPos [file :Bool part2] (let [lines (parseLines file) :Map 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 |#))) \ No newline at end of file