32 lines
929 B
Common Lisp
32 lines
929 B
Common Lisp
(ql:quickload :cl-ppcre)
|
|
|
|
(defun parse-input (input)
|
|
(ppcre:split "\\n" (uiop:read-file-string input)))
|
|
|
|
(defun get-height (trees line pos)
|
|
(digit-char-p (char (nth line trees) pos)))
|
|
|
|
(defun visible-p (trees line pos)
|
|
(cond ((zerop pos) 1)
|
|
((= pos (- (length (nth line trees)) 1)) 1)
|
|
((zerop line) 1)
|
|
((= line (- (length trees) 1)) 1)
|
|
(t
|
|
(let ((sum 0)
|
|
(height (get-height trees line pos))
|
|
(left (get-height trees line (- pos 1)))
|
|
(top (get-height trees (- line 1) pos))
|
|
(right (get-height trees line (+ pos 1)))
|
|
(bottom (get-height trees (+ line 1) pos)))
|
|
(cond ((> height left) (+ sum 1))
|
|
((> height top) (+ sum 1))
|
|
((> height right) (+ sum 1))
|
|
((> height bottom) (+ sum 1)))))))
|
|
|
|
(defun count-visible (trees)
|
|
(let ((sum 0))
|
|
(loop :for line :from 0 :to 98 :do
|
|
(loop :for pos :from 0 :to 98 :do
|
|
(if (visible-p trees line pos) (incf sum))))
|
|
sum))
|