;; https://adventofcode.com/2021/day/1 ;; *day1-input* is a text file of integer values delimited by a newline. (defun d1p1 () "Find the number of times the value of a line increases from the previous line." (with-open-file (stream *day1-input* :if-does-not-exist :error) (loop for line = (read-line stream NIL NIL) while line for prev = NIL then value for value = (parse-integer line) counting line into line-count counting (and prev (< prev value)) into count finally (return (values count line-count))))) ;; Answer: 1162. (defun d1p2 () "Find the number of times the sum of three lines increases from the sum of the previous sliding window of three lines." (with-open-file (stream *day1-input* :if-does-not-exist :error) (let ((values (make-array 3 :element-type 'integer :initial-contents '(0 0 0))) (index 0)) (labels ((sum () (loop for value across values summing value)) (next (line) (setf (aref values (mod index 3)) (parse-integer line)) (incf index) (sum))) ;; First three. (next (read-line stream)) (next (read-line stream)) (next (read-line stream)) (loop for line = (read-line stream NIL NIL) while line for prev = (sum) then current for current = (next line) counting (< prev current))))))) ;; Answer: 1190