advent-of-code-2024

My solutions to AoC 2024
Log | Files | Refs

day-1.lisp (951B)


      1 (defpackage #:aoc/day-1
      2   (:use #:cl #:aoc/utils)
      3   (:export #:day-1))
      4 (in-package #:aoc/day-1)
      5 
      6 (defun day-1 (input)
      7   (let ((right-counts (make-hash-table))
      8         left
      9         right)
     10     (loop for line = (read-line input nil)
     11           until (null line)
     12           for row = (read-number-list line)
     13           for right-num = (cadr row)
     14           do (push (car row) left)
     15              (push right-num right)
     16              (ensure-gethash right-num right-counts 0)
     17              (incf (gethash right-num right-counts))
     18           finally (setf left (sort left #'<)
     19                         right (sort right #'<)))
     20     (values
     21      (loop for number-left in left
     22            for number-right in right
     23            for distance = (abs (- number-left number-right))
     24            sum distance)
     25      (loop for number-left in left
     26            for number-of-instances = (or (gethash number-left right-counts) 0)
     27            sum (* number-left number-of-instances)))))