diff options
author | Emile <git@emile.space> | 2024-12-01 22:05:46 +0100 |
---|---|---|
committer | Emile <git@emile.space> | 2024-12-01 22:05:46 +0100 |
commit | 3577bb20ef3a3e1af25f1419a3e4a88d336866a0 (patch) | |
tree | c48ecf3d79695c54901883889b91b9ee0fdf8164 /2024/d01.lisp |
2024 day one
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 |