From 3577bb20ef3a3e1af25f1419a3e4a88d336866a0 Mon Sep 17 00:00:00 2001 From: Emile Date: Sun, 1 Dec 2024 22:05:46 +0100 Subject: 2024 day one --- 2024/d01.lisp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2024/d01.lisp (limited to '2024/d01.lisp') 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 -- cgit 1.4.1