Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
Created December 9, 2021 14:10
Show Gist options
  • Save commander-trashdin/9fd18cecb77a71eafd3cdec6bc6b0794 to your computer and use it in GitHub Desktop.
Save commander-trashdin/9fd18cecb77a71eafd3cdec6bc6b0794 to your computer and use it in GitHub Desktop.
AoC 2021, day 9
(defun read-lines (path)
(with-open-file (stream path :direction :input :if-does-not-exist :error)
(flet ((line->vector (line)
(let ((vec (make-array 0 :adjustable t :fill-pointer 0)))
(loop :for ch :across line
:do (vector-push-extend (- (char-code ch) 48) vec))
vec)))
(let ((res (make-array 0 :adjustable t :fill-pointer 0)))
(Loop :for line := (read-line stream nil nil)
:while line
:do (vector-push-extend (line->vector line) res))
res))))
(defun arref (vector i j)
(aref (aref vector i) j))
(defun check-neighbours (array i j)
(and (if (> i 0)
(< (arref array i j) (arref array (- i 1) j))
t)
(if (< i (- (length array) 1))
(< (arref array i j) (arref array (+ i 1) j))
t)
(if (> j 0)
(< (arref array i j) (arref array i (- j 1)))
t)
(if (< j (- (length (aref array 0)) 1))
(< (arref array i j) (arref array i (+ j 1)))
t)))
(defun day9.1 (path)
(let ((array (read-lines path)))
(loop :for i :from 0 :below (length array)
:sum (loop :for j :from 0 :below (length (aref array 0))
:when (check-neighbours array i j)
:sum (+ 1 (arref array i j))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment