aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/08/solver.el
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-09 01:04:14 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-09 01:04:14 +0100
commit2e42cd89007a613444de67cae0bc00c067dfdbb7 (patch)
treecf26d6f70066b2e6b8f7c66a23a3054cf8780836 /AoC2022/08/solver.el
parent23bbb7d957af6510479bce016f5ef7f8fdf6886e (diff)
downloadscratch-2e42cd89007a613444de67cae0bc00c067dfdbb7.tar.gz
scratch-2e42cd89007a613444de67cae0bc00c067dfdbb7.tar.bz2
scratch-2e42cd89007a613444de67cae0bc00c067dfdbb7.zip
[AoC2022] 08 elisp process eg
Diffstat (limited to 'AoC2022/08/solver.el')
-rw-r--r--AoC2022/08/solver.el79
1 files changed, 79 insertions, 0 deletions
diff --git a/AoC2022/08/solver.el b/AoC2022/08/solver.el
new file mode 100644
index 0000000..22bbfd2
--- /dev/null
+++ b/AoC2022/08/solver.el
@@ -0,0 +1,79 @@
+;;; solver.el --- Day 08 -*- lexical-binding: t; -*-
+;;
+;; Copyright (C) 2022 Óscar Nájera
+;;
+;; Author: Óscar Nájera <hi@oscarnajera.com>
+;; 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.
+;;
+;;; Commentary:
+;;
+;; Day 08
+;;
+;;; 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)))
+ (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))
+ (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))
+ (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))))))
+
+ (seq-partition
+ (cl-map 'vector #'identity
+ visibility) 5)
+ (cl-loop for v across visibility when v count it)
+ (apply #'vconcat forest)
+ ))