Plaster
New
List
Login
common-lisp
default
gingerale
2021.12.05 07:27:06
;; 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
Raw
Annotate
Repaste
Annotations
common-lisp
default
shinmera
2021.12.05 07:45:10
(defun read-values (file) (with-open-file (stream file) (loop for value = (read stream NIL) while value collect value))) (defun dip1 (file) (loop for (a b) on (read-values file) while b count (< a b))) (defun dip2 (file) (loop with window = 0 for (a b c) on (read-values file) while c count (< window (+ a b c)) do (setf window (+ a b c))))
Raw
Repaste
common-lisp
default
gingerale
2021.12.05 09:08:12
(defun d2-data () "Reads the file data. Every line of the file is of format '<direction> <unit>' where direction is either up, down, or forward, and unit is an integer." (with-open-file (stream *day2-input* :if-does-not-exist :error) (loop with scanner = (cl-ppcre:create-scanner "^(\\w+) (\\d+)$") for line = (read-line stream NIL) while line collect (cl-ppcre:register-groups-bind (direction value) (scanner line) (when (and direction value) (cons (intern (string-upcase direction) :keyword) (parse-integer value))))))) (defun d2p1 () "Calculates the submarine movement based on the commands from the file. Forward moves lengthwise, up reduces depth, and down increases depth. Note: Only the multiplication of the length and the depth is needed for the result. The rest is out of curiosity." (loop with length = 0 with depth = 0 for (direction . units) in (d2-data) do (ecase direction (:forward (incf length units)) (:up (decf depth units)) (:down (incf depth units))) finally (return (values (* length depth) length depth)))) (defun d2p2 () "Calculates the submarine movement based on the commands from the file. Forward moves lengthwise and depthwise, up decreases the angle of the aim, and down increases the angle of the aim. Note: Only the multiplication of the length and the depth is needed for the result. The rest is out of curiosity." (loop with length = 0 with depth = 0 with aim = 0 for (direction . units) in (d2-data) do (ecase direction (:forward (incf length units) (incf depth (* aim units))) (:up (decf aim units)) (:down (incf aim units))) finally (return (values (* length depth) (list :length length :depth depth :aim aim)))))
Raw
Repaste
common-lisp
default
shinmera
2021.12.05 09:46:03
(defun d2p1 (file) (loop with x = 0 and y = 0 for (direction amount) on (read-values file) by #'cddr do (ecase direction (forward (incf x amount)) (up (setf y (max 0 (- y amount)))) (down (incf y amount))) finally (return (* x y))))
Raw
Repaste