aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/15
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-17 13:32:48 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-17 13:32:48 +0100
commit3241a9f65f227f8bcbe53409b9138baa238fcace (patch)
tree14bf3537fa402dfc077ecf03bef2f4cfb26e846b /AoC2022/15
parenta017ee87f224d8fd20b370e4034a0159fbb4c115 (diff)
downloadscratch-3241a9f65f227f8bcbe53409b9138baa238fcace.tar.gz
scratch-3241a9f65f227f8bcbe53409b9138baa238fcace.tar.bz2
scratch-3241a9f65f227f8bcbe53409b9138baa238fcace.zip
Day 15 solve part 1
Diffstat (limited to 'AoC2022/15')
-rw-r--r--AoC2022/15/solver.lisp25
1 files changed, 25 insertions, 0 deletions
diff --git a/AoC2022/15/solver.lisp b/AoC2022/15/solver.lisp
index 0166b23..584141f 100644
--- a/AoC2022/15/solver.lisp
+++ b/AoC2022/15/solver.lisp
@@ -80,3 +80,28 @@
for l from (* (+ 10 ymin) stride)
when (= 3 (aref grid l))
count it))))
+
+(let* ((lines (uiop:read-file-lines "input"))
+ (markers (markers lines))
+ (bounds (bounds markers))
+ (max-distance (loop for (sensor beacon) on markers by #'cddr
+ maximize (manhattan-dist sensor beacon))))
+
+ (destructuring-bind ((xmin xmax) (ymin ymax)) bounds
+ (let* ((stride (- (+ (* 2 max-distance) xmax 1) xmin))
+ (row-min (- xmin max-distance))
+ (target-row 2000000)
+ (row (make-array stride :element-type '(unsigned-byte 1) :initial-element 0)))
+ (loop for (sensor beacon) on markers by #'cddr
+ for distance = (manhattan-dist sensor beacon)
+ when (= target-row (cdr sensor))
+ do (setf (aref row (- (car sensor) row-min)) 1)
+ when (= target-row (cdr beacon))
+ do (setf (aref row (- (car beacon) row-min)) 1)
+
+ sum (loop
+ for x from (- xmin distance) to (+ xmax distance)
+ when (and (<= (manhattan-dist sensor (cons x target-row)) distance)
+ (< -1 (- x row-min) stride)
+ (= 0 (aref row (- x row-min))))
+ count it and do (setf (aref row (- x row-min)) 1))))))