;;; 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 ;; ;; This file is not part of GNU Emacs. ;; ;;; Commentary: ;; ;; Day 08 ;; ;;; Code: (with-temp-buffer (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)) (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) (update-visibility (+ j (* forest-width i))))) ;; top visible (dotimes (j forest-depth) (setq last-max -1) (dotimes (i forest-width) (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) ))