aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-17 12:54:02 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-17 12:59:03 +0100
commita017ee87f224d8fd20b370e4034a0159fbb4c115 (patch)
tree382c77d586ac8234fc2e24e15b0f7d058df45c91
parent974ca5cf0f2979e03543816aabd54a51f9b3a071 (diff)
downloadscratch-a017ee87f224d8fd20b370e4034a0159fbb4c115.tar.gz
scratch-a017ee87f224d8fd20b370e4034a0159fbb4c115.tar.bz2
scratch-a017ee87f224d8fd20b370e4034a0159fbb4c115.zip
Day 15 CL eg monitor cover
-rw-r--r--AoC2022/15/solver.lisp61
1 files changed, 45 insertions, 16 deletions
diff --git a/AoC2022/15/solver.lisp b/AoC2022/15/solver.lisp
index 3d4fafd..0166b23 100644
--- a/AoC2022/15/solver.lisp
+++ b/AoC2022/15/solver.lisp
@@ -1,13 +1,6 @@
(ql:quickload '(fiveam str cl-ppcre arrows uiop))
-(setf markers
- (loop for line in (uiop:read-file-lines "eg-in")
- for (sx sy bx by) = (multiple-value-bind (match values)
- (cl-ppcre:scan-to-strings "Sensor at x=(-?\\d+), y=\(-?\\d+\): closest beacon is at x=(-?\\d+), y=(-?\\d+)"
- line)
- (map 'list #'parse-integer values))
- nconc (list (cons sx sy) (cons bx by))))
(defun bounds (markers)
(list (list
@@ -28,6 +21,13 @@
(when (and (<= xmin x xmax) (<= ymin y ymax))
(+ (- x xmin) (* (- y ymin) stride)))))))
+(defun from-grid (bounds)
+ (destructuring-bind ((xmin xmax) (ymin _)) bounds
+ (declare (ignore _))
+ (let ((stride (- xmax xmin -1)))
+ (lambda (pos)
+ (multiple-value-bind (y x) (floor pos stride)
+ (cons (+ x xmin) (+ y ymin)))))))
(defun draw-grid (grid stride)
(let ((out (make-string-output-stream)))
@@ -37,17 +37,46 @@
(princ (case elt
(0 ".")
(1 "S")
- (2 "B")) out)))
+ (2 "B")
+ (3 "#")) out)))
(get-output-stream-string out)))
-(let* ((bounds (bounds markers))
+(defun manhattan-dist (pos-sensor pos-beacon)
+ (destructuring-bind ((sx . sy) (bx . by)) (list pos-sensor pos-beacon)
+ (+ (abs (- bx sx)) (abs (- by sy)))))
+
+(defun markers (lines)
+ (loop for line in lines
+ for (sx sy bx by) = (multiple-value-bind (match values)
+ (cl-ppcre:scan-to-strings "Sensor at x=(-?\\d+), y=\(-?\\d+\): closest beacon is at x=(-?\\d+), y=(-?\\d+)"
+ line)
+ (declare (ignore match))
+ (map 'list #'parse-integer values))
+ nconc (list (cons sx sy) (cons bx by))))
+
+
+(let* ((lines (uiop:read-file-lines "eg-in"))
+ (markers (markers lines))
+ (bounds (bounds markers))
(loc (in-grid bounds))
- (grid (grid bounds)))
+ (grid (grid bounds))
+ (coords (from-grid bounds)))
(loop for ((sx . sy) (bx . by)) on markers by #'cddr
- do (progn (princ (list sx sy bx by))
- (setf (aref grid (funcall loc sx sy)) 1)
+ do (progn (setf (aref grid (funcall loc sx sy)) 1)
(setf (aref grid (funcall loc bx by)) 2)))
- (destructuring-bind ((xmin xmax) a) bounds
- (princ (draw-grid grid (- xmax xmin -1)))
- )
- )
+
+ (loop for (sensor beacon) on markers by #'cddr
+ for distance = (manhattan-dist sensor beacon)
+ do (loop
+ for elt across grid
+ and l from 0
+ when (and (<= (manhattan-dist sensor (funcall coords l)) distance) (= elt 0))
+ do (setf (aref grid l) 3)))
+
+ (destructuring-bind ((xmin xmax) (ymin ymax)) bounds
+ (let ((stride (- xmax xmin -1)))
+ ;; (princ (draw-grid grid stride))
+ (loop repeat stride
+ for l from (* (+ 10 ymin) stride)
+ when (= 3 (aref grid l))
+ count it))))