Skip to content

Instantly share code, notes, and snippets.

@death
Created December 2, 2021 05:22
Show Gist options
  • Select an option

  • Save death/d43d9fe9adb124d140d8135869ab488b to your computer and use it in GitHub Desktop.

Select an option

Save death/d43d9fe9adb124d140d8135869ab488b to your computer and use it in GitHub Desktop.
aoc2021-day2
;;;; +----------------------------------------------------------------+
;;;; | Advent of Code 2021 |
;;;; +----------------------------------------------------------------+
(defpackage #:snippets/aoc2021/day2
(:use #:cl)
(:export
#:day2))
(in-package #:snippets/aoc2021/day2)
(defmacro doinst (((op x) program) &body clauses)
`(loop for (,op ,x) on ,program by #'cddr
do (ecase ,op ,@clauses)))
(defun interpret1 (program)
(let ((pos 0)
(depth 0))
(doinst ((op x) program)
(:forward
(incf pos x))
(:down
(incf depth x))
(:up
(decf depth x)))
(* pos depth)))
(defun interpret2 (program)
(let ((pos 0)
(depth 0)
(aim 0))
(doinst ((op x) program)
(:forward
(incf pos x)
(incf depth (* aim x)))
(:down
(incf aim x))
(:up
(decf aim x)))
(* pos depth)))
(defun day2 (input)
(list (interpret1 input)
(interpret2 input)))
@death
Copy link
Copy Markdown
Author

death commented Dec 2, 2021

As usual, the input received by the main function is a transformation of the raw input (my regression tester is responsible for that). In this case I added a new function:

(defun read-as-list (string)
  "Return a list of objects READ from STRING."
  (with-standard-io-syntax
    (let ((*package* (find-package "KEYWORD"))
          (*read-eval* nil))
      (read
       (make-concatenated-stream
        (make-string-input-stream "(")
        (make-string-input-stream string)
        (make-string-input-stream ")"))))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment