diff options
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/08/solver.el | 88 |
1 files changed, 36 insertions, 52 deletions
diff --git a/AoC2022/08/solver.el b/AoC2022/08/solver.el index 22bbfd2..9c19651 100644 --- a/AoC2022/08/solver.el +++ b/AoC2022/08/solver.el @@ -6,10 +6,6 @@ ;; Maintainer: Óscar Nájera <hi@oscarnajera.com> ;; Created: December 09, 2022 ;; Modified: December 09, 2022 -;; Version: 0.0.1 -;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp -;; Homepage: https://github.com/titan/solver -;; Package-Requires: ((emacs "24.3")) ;; ;; This file is not part of GNU Emacs. ;; @@ -20,60 +16,48 @@ ;;; Code: (with-temp-buffer - (insert "30373 -25512 -65332 -33549 -35390") - (let* ((forest - (cl-map 'vector - (lambda (row) - (cl-map 'vector (lambda (col) (logxor col 48)) row)) ;; 48 is the ascii 0 - (split-string (buffer-string)))) - (forest-width (length forest)) - (forest-depth (length (aref forest 0))) + (insert-file-contents "input") + (let* ((rows (split-string (buffer-string))) + (forest-depth (length rows)) + (forest-width (length (car rows))) + (forest + (seq-mapcat + (lambda (row) + (cl-map 'vector (lambda (col) (logxor col 48)) row)) ;; 48 is the ascii 0 + rows 'vector)) (visibility - (make-bool-vector (* forest-width forest-depth) nil))) - ;; left visible - (dotimes (i forest-width) - (let ((last-max -1)) - (dotimes (j forest-depth) - (let* ((tree-height (aref (aref forest i) j)) - (mask-place (+ j (* forest-width i))) - (mask-val (aref visibility mask-place))) - (setf (aref visibility mask-place) (or mask-val (< last-max tree-height))) - (setq last-max (max last-max tree-height)))))) - ;; rigth visible - (dotimes (i forest-width) - (let ((last-max -1)) + (make-bool-vector (* forest-width forest-depth) nil)) + (last-max -1)) + (cl-flet ((update-visibility (index) + (let ((tree-height (aref forest index)) + (mask-val (aref visibility index))) + (setf (aref visibility index) (or mask-val (< last-max tree-height))) + (setq last-max (max last-max tree-height))))) + + + ;; left visible + (dotimes (i forest-width) + (setq last-max -1) (dotimes (j forest-depth) - (let* ((tree-height (aref (aref forest i) (- forest-depth j 1))) - (mask-place (+ (- forest-width j 1) (* forest-width i))) - (mask-val (aref visibility mask-place))) - (setf (aref visibility mask-place) (or mask-val (< last-max tree-height))) - (setq last-max (max last-max tree-height)))))) - ;; top visible - (dotimes (j forest-depth) - (let ((last-max -1)) - (dotimes (i forest-width) - (let* ((tree-height (aref (aref forest i) j)) - (mask-place (+ j (* forest-width i))) - (mask-val (aref visibility mask-place))) - (setf (aref visibility mask-place) (or mask-val (< last-max tree-height))) - (setq last-max (max last-max tree-height)))))) - ;; bottom visible - (dotimes (j forest-depth) - (let ((last-max -1)) + (update-visibility (+ j (* forest-width i))))) + ;; top visible + (dotimes (j forest-depth) + (setq last-max -1) (dotimes (i forest-width) - (let* ((tree-height (aref (aref forest (- forest-depth i 1)) j)) - (mask-place (+ j (* forest-width (- forest-depth i 1)))) - (mask-val (aref visibility mask-place))) - (setf (aref visibility mask-place) (or mask-val (< last-max tree-height))) - (setq last-max (max last-max tree-height)))))) + (update-visibility (+ j (* forest-width i))))) + ;; rigth visible + (dotimes (i forest-width) + (setq last-max -1) + (cl-loop for j downfrom (1- forest-depth) to 0 do + (update-visibility (+ j (* forest-width i))))) + ;; bottom visible + (dotimes (j forest-depth) + (setq last-max -1) + (cl-loop for i downfrom (1- forest-width) to 0 do + (update-visibility (+ j (* forest-width i)))))) (seq-partition (cl-map 'vector #'identity visibility) 5) (cl-loop for v across visibility when v count it) - (apply #'vconcat forest) )) |