From 9d945796591d2bf42ea3e78e84f927d93b3bffbc Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 13 Dec 2021 21:12:04 +0000 Subject: Adding initial files --- src/02.lisp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/02.lisp (limited to 'src/02.lisp') diff --git a/src/02.lisp b/src/02.lisp new file mode 100644 index 0000000..970e363 --- /dev/null +++ b/src/02.lisp @@ -0,0 +1,33 @@ +(load "~/quicklisp/setup.lisp") +(ql:quickload 'uiop :silent t) +(ql:quickload 'cl-ppcre :silent t) + +(defun get-file-lines (name) + (uiop:read-file-lines name)) + +;; Turn the input file into whatever form you will use for both parts +;; (get-file-lines) and (get-file-string) will be useful +(defun parse-input (input-file) + (let ((input (mapcar (lambda (s) (cl-ppcre:split " " s)) (get-file-lines input-file)))) + (mapcar (lambda (x) (cons (intern (string-upcase (CAR x))) (parse-integer (CADR x)))) input))) + +(defun part-a (parsed-input) + (let ((h 0) (v 0)) + (dolist (i parsed-input) + (case (car i) + (forward (incf h (cdr i))) + (up (decf v (cdr i))) + (down (incf v (cdr i))))) + (* h v))) + +(defun part-b (parsed-input) + (let ((h 0) (v 0) (a 0)) + (dolist (i parsed-input) + (case (car i) + (forward (progn (incf v (* (cdr i) a)) (incf h (cdr i)))) + (up (decf a (cdr i))) + (down (incf a (cdr i))))) + (* h v))) + +(time (format t "part 2: ~a~%" (part-a (parse-input "../inputs/02.txt")))) +(time (format t "part 2: ~a~%" (part-b (parse-input "../inputs/02.txt")))) -- cgit