blob: f20ffe5e65fbba1f0e8bf5f8d2fd3c432d8df2d9 (
plain) (
blame)
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
|
;;; https://gitlab.com/Syndamia/senzill
(require :senzill)
(use-package :senzill.math)
(use-package :senzill.collections)
(use-package :senzill.io)
(ask-for-stream (prog-input)
(let ((forest '()))
(doread-lines (inpt :read-line-options (prog-input NIL))
(push-back (map 'list
(lambda (x) (cons x 1)) ; Each tree is a (cons) pair of (<TREE_HEIGHT> . <SCENIC_SCORE>)
(integer-to-list (parse-integer inpt)
:min-length (length (first forest)))) ; Doesn't handle if very first digit(s) is zero
forest))
(defun loop-on-row (row)
(loop for col on row
maximize (car (first col)) into max-height
while (cdr col)
do
(*= (cdr (first col))
;;; Counts seen trees on the right of the current tree
(loop for seen on (cdr col)
count T into result
while (and col (> (car (first col)) (car (first seen))))
finally (return result)))
finally (setf (cdr (first col)) 0))) ; Last tree doesn't see any trees
;;; Checks every row
(loop for row in forest
do
;; Iterate from left to right
(loop-on-row row)
;; Iterate from right to left
(loop-on-row (reverse row)))
;;; Checks every column
(loop for i from 0 to (- (length (first forest)) 1)
for col = (loop for row in forest
collect (nth i row))
do
;; Iterate top to bottom
(loop-on-row col)
;; Iterate bottom to top
(loop-on-row (reverse col)))
(loop for row in forest
maximize (loop for tree in row
maximize (cdr tree)) into result
finally (print result))))
|