diff options
Diffstat (limited to '2024/d01.lisp')
-rw-r--r-- | 2024/d01.lisp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/2024/d01.lisp b/2024/d01.lisp new file mode 100644 index 0000000..b4ec6f2 --- /dev/null +++ b/2024/d01.lisp @@ -0,0 +1,36 @@ +(in-package :aoc/2024) + +(defparameter *input/d1* + (asdf:system-relative-pathname :advent "2024/d01.input")) + +(defun readlists (filename) + "Read the left and right columns of numbers into individual lists" + (let ((left-numbers '()) + (right-numbers '())) + (with-open-file (stream filename :direction :input) + (loop for line = (read-line stream nil nil) + while line do + (let* ((tab-pos (search " " line)) + (left (parse-integer (subseq line 0 tab-pos))) + (right (parse-integer (subseq line (1+ tab-pos))))) + (push left left-numbers) + (push right right-numbers)))) + (list + (nreverse left-numbers) + (nreverse right-numbers)))) + +(let* ((input-list (readlists *input/d1*)) + (left (sort (first input-list) #'<)) + (right (sort (second input-list) #'<)) + (diff (mapcar #'- left right)) + (absdiff (mapcar #'abs diff)) + (result (apply #'+ absdiff))) + (print result)) +; 2066446 + +(let* ((input-list (readlists *input/d1*)) + (left (first input-list)) + (right (second input-list)) + (result (apply #'+ (mapcar #'(lambda (x) (* x (count x right))) left)))) + (print result)) +; 24931009 |