(defun d2-data () "Reads the file data. Every line of the file is of format ' ' 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)))))