Created
December 2, 2021 05:22
-
-
Save death/d43d9fe9adb124d140d8135869ab488b to your computer and use it in GitHub Desktop.
aoc2021-day2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;;; +----------------------------------------------------------------+ | |
| ;;;; | 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))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: