commit 29afc863d60057ea2abb38447e69f545e294d22e Author: 4903000 <4903000@chiru.no> Date: Wed Dec 1 22:24:25 2021 +0000 Day 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ab4787 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +inputs/ \ No newline at end of file diff --git a/aoc21.lisp b/aoc21.lisp new file mode 100644 index 0000000..9c68eb5 --- /dev/null +++ b/aoc21.lisp @@ -0,0 +1,31 @@ +;;; Day 1 + +(defun read-1 (file) + (with-open-file (stream file) + (loop for line = (read-line stream nil) + while line + collect (parse-integer line)))) + +(defun solve-1-1 (file) + (let ((readings (read-1 file)) + (inc-acc 0)) + (labels ((rec (list old) + (if list + (progn + (if (< old (first list)) + (incf inc-acc)) + (rec (cdr list) (first list)))))) + (rec (cdr readings) (first readings)) + inc-acc))) + +(defun solve-1-2 (file) + (let ((readings (read-1 file)) + (inc-acc 0)) + (labels ((rec (list old-sum) + (if (cddr list) + (let ((sum (reduce #'+ (subseq list 0 3)))) + (if (< old-sum sum) + (incf inc-acc)) + (rec (cdr list) sum))))) + (rec (cdr readings) (reduce #'+ (subseq readings 0 3))) + inc-acc)))