This commit is contained in:
Alan Daniels 2025-12-06 16:56:05 +11:00
parent 46f10c043e
commit 2e19753b65

View file

@ -4,18 +4,14 @@
(defun get-range (range) (defun get-range (range)
(let ((range (str:split "-" range))) (let ((range (str:split "-" range)))
(loop for i from (parse-integer (car range)) to (parse-integer (car (cdr range))) collect i) (loop for i from (parse-integer (car range)) to (parse-integer (car (cdr range))) collect i)))
)
)
(get-range "11-22") (get-range "11-22")
(defun get-ranges (instr) (defun get-ranges (instr)
(loop for range in (loop for range in
(str:split "," (string-trim '(#\Newline) instr)) (str:split "," (string-trim '(#\Newline) instr))
collect collect
(get-range range) (get-range range)))
)
)
(get-ranges "1-3,4-5") (get-ranges "1-3,4-5")
(defun validate-product-id (product-id) (defun validate-product-id (product-id)
@ -25,10 +21,7 @@
;(print product-id) ;(print product-id)
(cond (cond
((not (evenp product-id-length)) t) ((not (evenp product-id-length)) t)
(t (not (string= (subseq product-id 0 split-at) (subseq product-id split-at)))) (t (not (string= (subseq product-id 0 split-at) (subseq product-id split-at)))))))
)
)
)
(validate-product-id 1212) (validate-product-id 1212)
(validate-product-id 123) (validate-product-id 123)
(validate-product-id 1234) (validate-product-id 1234)
@ -37,11 +30,7 @@
(reduce #'+ (reduce #'+
(remove-if #'validate-product-id (remove-if #'validate-product-id
(reduce #'append (reduce #'append
(get-ranges text) (get-ranges text)))))
)
)
)
)
(sum-all-invalid-ids (uiop:read-file-string "02example.txt")) (sum-all-invalid-ids (uiop:read-file-string "02example.txt"))
(sum-all-invalid-ids (uiop:read-file-string "02data.txt")) (sum-all-invalid-ids (uiop:read-file-string "02data.txt"))