diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-13 18:47:13 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-13 18:47:13 +0100 |
commit | 7aedef417bb6e0bf885c6af8cdfef44fe340d345 (patch) | |
tree | 987c4a906130ea899cead1d6f8de0f38ecd61248 | |
parent | 53fe869e55b2390cba8caef45a2a9559db365386 (diff) | |
download | scratch-7aedef417bb6e0bf885c6af8cdfef44fe340d345.tar.gz scratch-7aedef417bb6e0bf885c6af8cdfef44fe340d345.tar.bz2 scratch-7aedef417bb6e0bf885c6af8cdfef44fe340d345.zip |
CL group in functions
-rw-r--r-- | AoC2022/08/solver.lisp | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/AoC2022/08/solver.lisp b/AoC2022/08/solver.lisp index e3b63e5..9031e19 100644 --- a/AoC2022/08/solver.lisp +++ b/AoC2022/08/solver.lisp @@ -1,11 +1,16 @@ (ql:quickload '(fiveam uiop)) -(defun coord (width height) +(defun coord-x-minor (width height) (lambda (x y) (when (and (< -1 x width) (< -1 y height)) (+ x (* y width))))) +(defun coord-y-minor (width height) + (let ((xminor (coord-x-minor width height))) + (lambda (y x) + (funcall xminor x y)))) + (let* ((data (uiop:read-file-lines "input")) (width (length (car data))) (height (length data)) @@ -14,37 +19,27 @@ (mapcan (lambda (row) (map 'list (lambda (x) (- (char-code x) 48)) row)) data))) (visibility-mask (make-array (* width height) :initial-element nil)) - (location (coord width height))) - (dotimes (y height) - ;; left watch - (loop - for x from 0 below width - for maxh = -1 then (max maxh tree-height) - for tree-height = (aref forest-arr (funcall location x y)) - when (> tree-height maxh) - do (setf (aref visibility-mask (funcall location x y)) t)) - ;; right watch - (loop - for x downfrom (1- width) to 0 - for maxh = -1 then (max maxh tree-height) - for tree-height = (aref forest-arr (funcall location x y)) - when (> tree-height maxh) - do (setf (aref visibility-mask (funcall location x y)) t))) + (location-x (coord-x-minor width height)) + (location-y (coord-y-minor width height)) + ) + (flet ((toggle-visibility (major minor location reverse) + (dotimes (m major) + (let ((range (loop for i below minor collect i))) + (loop + for n in (if reverse (nreverse range) range) + for maxh = -1 then (max maxh tree-height) + for tree-height = (aref forest-arr (funcall location n m)) + when (> tree-height maxh) + do (setf (aref visibility-mask (funcall location n m)) t)))))) + (toggle-visibility height width location-x nil) + (toggle-visibility height width location-x t) + (toggle-visibility width height location-y nil) + (toggle-visibility width height location-y t) + ) + + - (dotimes (x width) - ;; top watch - (loop - for y from 0 below height - for maxh = -1 then (max maxh tree-height) - for tree-height = (aref forest-arr (funcall location x y)) - when (> tree-height maxh) - do (setf (aref visibility-mask (funcall location x y)) t)) - ;; bottom watch - (loop - for y downfrom (1- height) to 0 - for maxh = -1 then (max maxh tree-height) - for tree-height = (aref forest-arr (funcall location x y)) - when (> tree-height maxh) - do (setf (aref visibility-mask (funcall location x y)) t))) - (loop for v across visibility-mask counting v)) + (loop for v across visibility-mask counting v) + ;; visibility-mask + ) |