aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/08/solver.el
blob: 9c19651e7d92a0c4451d0647263e1d407dec8ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
;;; 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
;;
;; 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)
    ))