aoc-2025/02a.lisp
Alan Daniels 2e19753b65 fmt
2025-12-06 16:56:05 +11:00

36 lines
1.1 KiB
Common Lisp

(require :uiop)
(load (sb-ext:posix-getenv "ASDF"))
(asdf:load-system 'str)
(defun get-range (range)
(let ((range (str:split "-" range)))
(loop for i from (parse-integer (car range)) to (parse-integer (car (cdr range))) collect i)))
(get-range "11-22")
(defun get-ranges (instr)
(loop for range in
(str:split "," (string-trim '(#\Newline) instr))
collect
(get-range range)))
(get-ranges "1-3,4-5")
(defun validate-product-id (product-id)
(let ((product-id (format nil "~A" product-id)))
(setq product-id-length (length product-id))
(setq split-at (/ product-id-length 2))
;(print product-id)
(cond
((not (evenp product-id-length)) t)
(t (not (string= (subseq product-id 0 split-at) (subseq product-id split-at)))))))
(validate-product-id 1212)
(validate-product-id 123)
(validate-product-id 1234)
(defun sum-all-invalid-ids (text)
(reduce #'+
(remove-if #'validate-product-id
(reduce #'append
(get-ranges text)))))
(sum-all-invalid-ids (uiop:read-file-string "02example.txt"))
(sum-all-invalid-ids (uiop:read-file-string "02data.txt"))