From 225cbc55a4992e3b656129fe75e51a139bee2d07 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 2 Dec 2024 22:33:06 +0100 Subject: Add an Emacs Lisp solution --- 2024/01/puzzle-1.el | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2024/01/puzzle-1.el (limited to '2024/01') diff --git a/2024/01/puzzle-1.el b/2024/01/puzzle-1.el new file mode 100644 index 0000000..cd8853e --- /dev/null +++ b/2024/01/puzzle-1.el @@ -0,0 +1,21 @@ +(defun solve (input-file) + (let* ((nums (with-temp-buffer + (insert-file-contents-literally input-file) + (goto-char (point-min)) + (save-excursion + (insert ?\[) + (goto-char (point-max)) + (insert ?\])) + (read (current-buffer)))) + (num-count (length nums))) + (cl-loop for i from 0 below num-count + if (cl-evenp i) + collect (aref nums i) into xs + else + collect (aref nums i) into ys + finally return (thread-last + (seq-mapn #'- (sort xs) (sort ys)) + (mapcar #'abs) + (apply #'+))))) + +(message "%d" (solve "input")) \ No newline at end of file -- cgit v1.2.3