Inject code that should be executed for each cell
This commit is contained in:
parent
269a3bbb87
commit
574c6ed158
33
2022.org
33
2022.org
|
@ -9104,21 +9104,21 @@ Consider your map; how many trees are visible from outside the grid?
|
|||
vmap))
|
||||
|
||||
(defmacro advent/8/count-trees-from-edge ()
|
||||
'(let ((h (advent/8/height-at hmap m n))
|
||||
(vmap-update (apply-partially #'advent/8/update-vmap vmap m n))
|
||||
(max-vert (or (advent/8/max-height-at vmap (+ m (* -1 step)) n vprop) 0))
|
||||
(max-hor (or (advent/8/max-height-at vmap m (+ n (* -1 step)) hprop) 0))
|
||||
(max-heights (-non-nil (list
|
||||
(plist-get (gethash (cons m (1- n)) vmap) :left)
|
||||
(plist-get (gethash (cons m (1+ n)) vmap) :right)
|
||||
(plist-get (gethash (cons (1- m) n) vmap) :top)
|
||||
(plist-get (gethash (cons (1+ m) n) vmap) :bottom)))))
|
||||
(setq vmap (funcall vmap-update hprop (max h max-hor)))
|
||||
(setq vmap (funcall vmap-update vprop (max h max-vert)))
|
||||
(setq vmap (funcall vmap-update :visible (advent/8/tree-is-visible hmap m n max-heights)))))
|
||||
''(let ((h (advent/8/height-at hmap m n))
|
||||
(vmap-update (apply-partially #'advent/8/update-vmap vmap m n))
|
||||
(max-vert (or (advent/8/max-height-at vmap (+ m (* -1 step)) n vprop) 0))
|
||||
(max-hor (or (advent/8/max-height-at vmap m (+ n (* -1 step)) hprop) 0))
|
||||
(max-heights (-non-nil (list
|
||||
(plist-get (gethash (cons m (1- n)) vmap) :left)
|
||||
(plist-get (gethash (cons m (1+ n)) vmap) :right)
|
||||
(plist-get (gethash (cons (1- m) n) vmap) :top)
|
||||
(plist-get (gethash (cons (1+ m) n) vmap) :bottom)))))
|
||||
(setq vmap (funcall vmap-update hprop (max h max-hor)))
|
||||
(setq vmap (funcall vmap-update vprop (max h max-vert)))
|
||||
(setq vmap (funcall vmap-update :visible (advent/8/tree-is-visible hmap m n max-heights)))))
|
||||
|
||||
(defun advent/8/get-visibility-map (hmap)
|
||||
"Transforms the height map HMAP into a visibility map."
|
||||
(defun advent/8/get-visibility-map (hmap process-cell-form)
|
||||
"Transforms the height map HMAP into a visibility map. Each cell is evaluated through PROCESS-CELL-FORM."
|
||||
(let ((vmap (make-hash-table :test 'equal))
|
||||
(M (length hmap))
|
||||
(N (length (car hmap))))
|
||||
|
@ -9146,7 +9146,7 @@ Consider your map; how many trees are visible from outside the grid?
|
|||
(update-coordinate (1- m) (1- N) step hprop vprop))
|
||||
|
||||
;; process a cell that's within bounds
|
||||
(t (advent/8/count-trees-from-edge)
|
||||
(t (eval process-cell-form)
|
||||
(update-coordinate m (+ n step) step hprop vprop))))))
|
||||
|
||||
(defun advent/8/parse-line (line)
|
||||
|
@ -9162,7 +9162,8 @@ Consider your map; how many trees are visible from outside the grid?
|
|||
|
||||
(defun advent/8/count-visible-trees (input)
|
||||
(let* ((hmap (advent/8/parse-input input))
|
||||
(vmap (advent/8/get-visibility-map hmap)))
|
||||
(vmap (advent/8/get-visibility-map hmap
|
||||
(advent/8/count-trees-from-edge))))
|
||||
(-count (lambda (cell) (plist-get cell :visible))
|
||||
(hash-table-values vmap))))
|
||||
#+end_src
|
||||
|
|
Loading…
Reference in a new issue