advent-of-code-common-lisp/2024/day-01/stage-1.lisp

27 lines
853 B
Common Lisp
Raw Normal View History

2024-12-04 01:22:54 +01:00
(require 'asdf)
(asdf:load-system "cl-strings")
(require :cl-strings)
(defun diff (data)
(let* ((lists (reduce (lambda (acc itm) (cons (cons (parse-integer (car itm)) (car acc)) (cons (parse-integer (cadr itm)) (cdr acc))))
(mapcar (lambda (line) (cl-strings:split line " "))
(cl-strings:split data "
"))
:initial-value (cons (list) (list))))
(sorted (cons (sort (copy-seq (car lists)) #'<)
(sort (copy-seq (cdr lists)) #'<))))
(reduce #'+ (mapcar (lambda (itm1 itm2) (abs (- itm1 itm2)))
(car sorted)
(cdr sorted)))))
(format t "~s~%" (diff "3 4
4 3
2 5
1 3
3 9
3 3"))
(format t "~s~%" (diff (string-right-trim '(#\Space #\Newline) (uiop:read-file-string "input"))))