day 2 part 1
This commit is contained in:
parent
920f9032dd
commit
46f10c043e
4 changed files with 107 additions and 0 deletions
47
02a.lisp
Normal file
47
02a.lisp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(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"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue