1(defpackage #:advent.2024.01
2 (:use #:cl))
3(in-package #:advent.2024.01)
4
5;;; Part 1
6;; What is the total difference between your lists?
7
8(defun read-input (file)
9 "Read an input FILE that's 2 columns of numbers.
10Return each column separately."
11 (loop for line in (uiop:read-file-lines file)
12 for (a b) = (ppcre:split "\\s+" line)
13 collect a into list-a
14 collect b into list-b
15 finally (return (values
16 (mapcar #'serapeum:parse-number list-a)
17 (mapcar #'serapeum:parse-number list-b)))))
18
19(defvar *input* "01.input")
20
21(defun total-distance ()
22 (multiple-value-bind (left right) (read-input *input*)
23 (apply #'+
24 (mapcar (lambda (a b)
25 (abs (- a b)))
26 (sort left #'<)
27 (sort right #'<)))))
28
29;;; Part 2
30
31(defun similarity-score ()
32 (multiple-value-bind (left right) (read-input *input*)
33 (loop for n in left
34 sum (* n (loop for m in right
35 count (= n m))))))