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/01.lisp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/01.lisp (limited to 'src/01.lisp') diff --git a/src/01.lisp b/src/01.lisp new file mode 100644 index 0000000..9646e40 --- /dev/null +++ b/src/01.lisp @@ -0,0 +1,24 @@ +(load "~/quicklisp/setup.lisp") +(ql:quickload "uiop") + +(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) + (mapcar 'parse-integer (get-file-lines input-file))) + +;; Loops through two item windows on input and counts each time the first is less than the second +(defun part-a (parsed-input) + (let ((sum 0) (prev 0)) + (dolist (i parsed-input) + (if (> i prev) (progn (incf sum 1) (setf prev i)))) + sum)) + +;; Similar to part a, but with four item windows, comparing the sum of the first 3 with the next 3 +(defun part-b (parsed-input) + (loop for (w x y z) on parsed-input until (null z) if (< (+ w x y) (+ x y z)) count w)) + +(time (format t "part 1: ~a~%" (part-a (parse-input "../inputs/01.txt")))) +(time (format t "part 1: ~a~%" (part-b (parse-input "../inputs/01.txt")))) -- cgit