summaryrefslogtreecommitdiffstats
path: root/src/02.lisp
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2021-12-13 21:12:04 +0000
committerYann Herklotz <git@yannherklotz.com>2021-12-13 21:12:04 +0000
commit9d945796591d2bf42ea3e78e84f927d93b3bffbc (patch)
tree36efe9567ddbd649f40917fa4972faa3994ca96b /src/02.lisp
downloadaoc21-9d945796591d2bf42ea3e78e84f927d93b3bffbc.tar.gz
aoc21-9d945796591d2bf42ea3e78e84f927d93b3bffbc.zip
Adding initial files
Diffstat (limited to 'src/02.lisp')
-rw-r--r--src/02.lisp33
1 files changed, 33 insertions, 0 deletions
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"))))