diff options
Diffstat (limited to 'AoC2022/15')
-rw-r--r-- | AoC2022/15/eg-in | 14 | ||||
-rw-r--r-- | AoC2022/15/solver.lisp | 53 |
2 files changed, 67 insertions, 0 deletions
diff --git a/AoC2022/15/eg-in b/AoC2022/15/eg-in new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/AoC2022/15/eg-in @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 diff --git a/AoC2022/15/solver.lisp b/AoC2022/15/solver.lisp new file mode 100644 index 0000000..3d4fafd --- /dev/null +++ b/AoC2022/15/solver.lisp @@ -0,0 +1,53 @@ +(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 + (loop for (x . y) in markers minimize x) + (loop for (x . y) in markers maximize x)) + (list + (loop for (x . y) in markers minimize y) + (loop for (x . y) in markers maximize y)))) + +(defun grid (bounds) + (destructuring-bind ((xmin xmax) (ymin ymax)) bounds + (make-array (* (- xmax xmin -1) (- ymax ymin -1)) :initial-element 0 :element-type '(unsigned-byte 2)))) + +(defun in-grid (bounds) + (destructuring-bind ((xmin xmax) (ymin ymax)) bounds + (let ((stride (- xmax xmin -1))) + (lambda (x y) + (when (and (<= xmin x xmax) (<= ymin y ymax)) + (+ (- x xmin) (* (- y ymin) stride))))))) + + +(defun draw-grid (grid stride) + (let ((out (make-string-output-stream))) + (loop for elt across grid + for idx from 0 + do (progn (when (= 0 (mod idx stride)) (terpri out)) + (princ (case elt + (0 ".") + (1 "S") + (2 "B")) out))) + (get-output-stream-string out))) + +(let* ((bounds (bounds markers)) + (loc (in-grid bounds)) + (grid (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) + (setf (aref grid (funcall loc bx by)) 2))) + (destructuring-bind ((xmin xmax) a) bounds + (princ (draw-grid grid (- xmax xmin -1))) + ) + ) |