Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created December 3, 2021 08:16
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 justinabrahms/30eb516358bc72a7e16b802136b8b025 to your computer and use it in GitHub Desktop.
Save justinabrahms/30eb516358bc72a7e16b802136b8b025 to your computer and use it in GitHub Desktop.
;; add paredit mode to lisp mode
(require "fiveam")
(require "log")
(defpackage :aoc/two
(:use #:cl)
(:import-from #:fiveam
#:def-suite*
#:test
#:run!
#:is))
(in-package :aoc/two)
(defun horiz (acc)
(car acc))
(defun depth (acc)
(cadr acc))
(defun forward (i acc)
(destructuring-bind (horiz depth func) acc
(list (+ horiz i) depth nil)))
(defun backwards (i acc)
`(,(- (horiz acc) i)
,(depth acc)
nil)
)
(defun up (i acc)
`(,(horiz acc)
,(- (depth acc) i)
nil)
)
(defun down (i acc)
`(,(horiz acc)
,(+ (depth acc) i)
nil)
)
(defun navigate (horiz depth input)
(reduce
(lambda (acc i)
;; (log:info "ACC is " acc " and I is " i)
(if (caddr acc) ;; if we have a function to apply. `i` is a number.
(funcall (caddr acc) (parse-integer i) acc)
;; else determine the function to add to acc[2]
(progn
;; ugh. this is a confusing section!
(rplacd (cdr acc) (list (find-symbol (string-upcase i))))
acc)))
input
;; An object would be better here, but I want to track depth info & next
;; instruction if any
:initial-value (list horiz depth nil)))
(defun day-one-answer (input)
(let ((acc (navigate 0 0 input)))
(* (horiz acc) (depth acc))))
(def-suite* :aoc/two)
(test forward
(is (equal (navigate 0 0 '("forward" "2")) '(2 0 nil))))
(test backwards
(is (equal (navigate 0 0 '("backwards" "2")) '(-2 0 nil))))
(test up
(is (equal (navigate 0 0 '("up" "2")) '(0 -2 nil))))
(test down
(is (equal (navigate 0 0 '("down" "2")) '(0 2 nil))))
(test noop
(is (equal (navigate 0 0 '("down" "1" "up" "1")) '(0 0 nil))))
(run! :aoc/two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment