From 2e42cd89007a613444de67cae0bc00c067dfdbb7 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Fri, 9 Dec 2022 01:04:14 +0100 Subject: [AoC2022] 08 elisp process eg --- AoC2022/08/solver.el | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 AoC2022/08/solver.el (limited to 'AoC2022/08/solver.el') 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 +;; Maintainer: Óscar Nájera +;; 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) + )) -- cgit v1.2.3