I have recently started learning lisp by reading ANSI Common Lisp, and I thought advent of code would be good for practice. Any feedback on my code for Day 1 would be appreciated.
(require :uiop)
(defparameter *input-file* "01-input")
(defparameter *input-lines* (uiop:read-file-lines *input-file*))
(defun solve ()
(multiple-value-bind (xs ys) (parse-input-lines *input-lines*)
(format t "Part 1: ~a~%" (part-1 xs ys))
(format t "Part 2: ~a~%" (part-2 xs ys))))
(defun parse-input-lines (lines)
(let (xs ys)
(loop
for line in lines
do (with-input-from-string (s line)
(push (read s) xs)
(push (read s) ys)))
(values (sort xs #'<) (sort ys #'<))))
(defun part-1 (xs ys)
(reduce #'+ (mapcar (lambda (x y) (abs (- x y))) xs ys)))
(defun part-2 (xs ys)
(let ((counts (make-hash-table)))
(loop for y in ys
do (setf (gethash y counts) (1+ (or (gethash y counts) 0))))
(loop for x in xs
sum (* x (or (gethash x counts) 0)) into similarity-score
finally (return similarity-score))))
*input-file*, with earmuffs, looks great. But the*input-lines*global feels unnatural. Prefer to just leave it as an anonymous expression within thatmultiple-value-bind. \$\endgroup\$