git.mcksp
    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
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
(defun read-grid (filename)
  (with-open-file (stream filename)
    (let ((rows (loop for line = (read-line stream nil)
                      while line
                      collect (map 'list
                                   (lambda (c) (char= c #\#))
                                   line))))
      (make-array (list (length rows)
                        (length (first rows)))
                  :initial-contents rows))))

(defun part-one (grid rows cols)
  (let ((result 0))
    (loop for r below (- rows 1) do
      (loop for c below (- cols 1) do
        (when (aref grid r c)
          (let ((r2 (+ r 1))
                (c2 (+ c 1)))
            (loop while (< r2 rows)
                  while (< c2 cols) do
              (when (and (aref grid r2 c2) (aref grid r c2) (aref grid r2 c))
                (incf result)
              )
              (incf r2)
              (incf c2)
            )
          )
        )
      )
    )
    result
  )
)

(defun safe-check (grid rows cols r c)
  (if (and (>= r 0) (>= c 0) (< r rows) (< c cols))
    (aref grid r c)
    nil
  )
)

(defun part-two (grid rows cols)
  (let ((result 0))
    (loop for r below (- rows 1) do
      (loop for c below (- cols 1) do
        (when (aref grid r c)
          (loop for rdelta from 0 to (- rows (+ r 1)) do
            (loop for cdelta from 1 to (- cols (+ c 1)) do
              (when (and (safe-check grid rows cols (+ r rdelta) (+ c cdelta))
                       (safe-check grid rows cols (+ r cdelta) (- c rdelta))
                       (safe-check grid rows cols (+ r (+ rdelta cdelta)) (+ c (- cdelta rdelta))))
                (incf result)
              )
            )
          )
        )
      )
    )
    result
  )
)


(defun main ()
  (let* ((grid (read-grid "input.txt"))
         (rows (array-dimension grid 0))
         (cols (array-dimension grid 1)))
    (format t "~a~%" (part-one grid rows cols))
    (format t "~a~%" (part-two grid rows cols))
  )
)

(main)