aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-17 20:42:13 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-17 20:42:13 +0100
commit3aa1e31d5b6b358b0701f24a7f33ef166b0914e0 (patch)
tree47fbe2a37b261d842a7fcc5dfa144fb5a00c42f5
parentf32f774eaec01d604f1a48055296716a49ebb678 (diff)
downloadadvent-of-code-3aa1e31d5b6b358b0701f24a7f33ef166b0914e0.tar.gz
advent-of-code-3aa1e31d5b6b358b0701f24a7f33ef166b0914e0.tar.bz2
advent-of-code-3aa1e31d5b6b358b0701f24a7f33ef166b0914e0.zip
Finished round 1 of puzzle-8
-rw-r--r--2022/puzzle-8.lisp45
1 files changed, 45 insertions, 0 deletions
diff --git a/2022/puzzle-8.lisp b/2022/puzzle-8.lisp
index 426ae13..38c5421 100644
--- a/2022/puzzle-8.lisp
+++ b/2022/puzzle-8.lisp
@@ -5,3 +5,48 @@
(defparameter trees
(mapcar (lambda (line) (mapcar (lambda (char) (parse-integer (string char))) (string-to-clist line)))
lines))
+;; Size of a row or column
+(defparameter n-trees (length trees))
+
+(defun get-column (x)
+ (loop for row in trees
+ collect (nth x row)))
+
+(defun get-row (y)
+ (nth y trees))
+
+(defun get-tree (x y)
+ (nth x (nth y trees)))
+
+(defun is-visible? (x y)
+ (if (or (= x 0) (= x (- n-trees 1))
+ (= y 0) (= y (- n-trees 1)))
+ t
+ (let* ((tree (get-tree x y))
+ (lt-tree? (lambda (a) (< a tree)))
+ (row (get-row y))
+ (row-left (subseq row 0 x))
+ (row-right (subseq row (+ x 1)))
+ (col (get-column x))
+ (col-top (subseq col 0 y))
+ (col-bottom (subseq col (+ y 1))))
+ (or
+ (all lt-tree? row-left)
+ (all lt-tree? row-right)
+ (all lt-tree? col-top)
+ (all lt-tree? col-bottom)))))
+
+(defun how-many-visible ()
+ (loop
+ for x from 0 to (- n-trees 1)
+ sum
+ (reduce
+ #'+
+ (loop
+ for y from 0 to (- n-trees 1)
+ if (is-visible? x y)
+ collect 1
+ else
+ collect 0))))
+
+(format t "round 1: ~s~%" (how-many-visible))