Solve AOC day 5 pt 2
This commit is contained in:
@@ -29,7 +29,9 @@
|
|||||||
(day 5
|
(day 5
|
||||||
(load "day5.kiss")
|
(load "day5.kiss")
|
||||||
(assert (= 5 ~(numHotPositions "src/year2021/inputs/day5-example.txt")))
|
(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
|
(day 6
|
||||||
(load "day6.kiss")
|
(load "day6.kiss")
|
||||||
(assert (= "5934" (countAfter "src/year2021/inputs/day6-example.txt" 80)))
|
(assert (= "5934" (countAfter "src/year2021/inputs/day6-example.txt" 80)))
|
||||||
|
|||||||
@@ -6,30 +6,35 @@
|
|||||||
(objectWith x1 y1 x2 y2))))
|
(objectWith x1 y1 x2 y2))))
|
||||||
|
|
||||||
// An inclusive range whose start may be greater than its end
|
// 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)
|
(let [s (min start end)
|
||||||
e (max 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
|
(cond
|
||||||
((= line.x1 line.x2)
|
((= line.x1 line.x2)
|
||||||
(for y (inclusiveRange line.y1 line.y2) [line.x1 y]))
|
(for y (inclusiveRange line.y1 line.y2) [line.x1 y]))
|
||||||
((= line.y1 line.y2)
|
((= line.y1 line.y2)
|
||||||
(for x (inclusiveRange line.x1 line.x2) [x line.y1]))
|
(for x (inclusiveRange line.x1 line.x2) [x line.y1]))
|
||||||
// Diagonal lines:
|
// Diagonal lines:
|
||||||
|
(part2
|
||||||
|
(zipThrow (inclusiveRange line.x1 line.x2) (inclusiveRange line.y1 line.y2)))
|
||||||
(true
|
(true
|
||||||
[])))
|
[])))
|
||||||
|
|
||||||
(function :Map<String,Int64> ventsByPos [file]
|
(function :Map<String,Int64> ventsByPos [file :Bool part2]
|
||||||
(let [lines (parseLines file)
|
(let [lines (parseLines file)
|
||||||
:Map<String,Int64> theMap (new Map)]
|
:Map<String,Int64> theMap (new Map)]
|
||||||
(doFor line lines
|
(doFor line lines
|
||||||
(doFor point (pointsCoveredBy line)
|
(doFor point (pointsCoveredBy line part2)
|
||||||
(dictInc theMap (Std.string point) 1)))
|
(dictInc theMap (Std.string point) 1)))
|
||||||
theMap))
|
theMap))
|
||||||
|
|
||||||
(function :Int numHotPositions [file]
|
(function :Int numHotPositions [file &opt :Bool part2]
|
||||||
(let [theMap (ventsByPos file)
|
(let [theMap (ventsByPos file ?part2)
|
||||||
two (Int64Helper.fromFloat 2)]
|
two (Int64Helper.fromFloat 2)]
|
||||||
(count theMap ->vents #| vents >= two |#)))
|
(count theMap ->vents #| vents >= two |#)))
|
||||||
Reference in New Issue
Block a user