Skip to content

Instantly share code, notes, and snippets.

@abishek
Created Dec 2, 2021
Embed
What would you like to do?
My solution for day 02 of advent of code 2021.
(defun read-input (filename)
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
collect line)))
(defun move-command (command x y)
"A command has two parts. the direction and the count."
(let* ((dir-and-steps (cl-ppcre:split "\\s+" command))
(dir (first dir-and-steps))
(steps (parse-integer (second dir-and-steps))))
(cond ((string-equal dir "forward") (values (+ x steps) y))
((string-equal dir "up") (values x (- y steps)))
((string-equal dir "down") (values x (+ y steps)))
(t (error "I give up.")))))
(defun updated-move-command (command x y aim)
"A command has two parts. the direction and the count."
(let* ((dir-and-steps (cl-ppcre:split "\\s+" command))
(dir (first dir-and-steps))
(steps (parse-integer (second dir-and-steps))))
(cond ((string-equal dir "forward") (values (+ x steps) (+ y (* steps aim)) aim))
((string-equal dir "up") (values x y (- aim steps)))
((string-equal dir "down") (values x y (+ aim steps)))
(t (error "I give up.")))))
(defun process-input-commands (commands)
(let ((x 0)
(y 0)
(aim 0))
(loop for command in commands do
(and
(setf (values x y aim) (updated-move-command command x y aim))
(format t "x: ~a, y: ~a, aim: ~a~%" x y aim)))
(* x y)))
(process-input-commands (read-input "day02.txt"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment