Skip to content

Instantly share code, notes, and snippets.

@abishek
Created December 2, 2021 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abishek/04d828683301427427ecb79ffd73f955 to your computer and use it in GitHub Desktop.
Save abishek/04d828683301427427ecb79ffd73f955 to your computer and use it in GitHub Desktop.
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