this post was submitted on 05 Dec 2025
21 points (100.0% liked)

Advent Of Code

1199 readers
3 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 5: Cafeteria

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] ystael@beehaw.org 2 points 2 weeks ago

Nothing interesting here. I was tripped up momentarily by the fact that Common Lisp sort, while it's a destructive operation, does not leave its input argument equal to its result! Typically you see either "nondestructive, returns a value" (Python sorted()) or "destructive, leaves the object in the final state" (Python list.sort()). Old school Lisp sort returns the sorted list and the input structure is not guaranteed to be meaningful afterward. Hope you weren't using that pair for anything; it now points to hell.

(ql:quickload :str)

(defun read-inputs (filename)
  (let* ((input-lines (uiop:read-file-lines filename))
         (range-lines (remove-if-not #'(lambda (s) (find #\- s)) input-lines))
         (id-lines (remove-if #'(lambda (s) (or (zerop (length s)) (find #\- s))) input-lines)))
    (flet ((parse-range (line) (mapcar #'parse-integer (str:split "-" line))))
      (cons (mapcar #'parse-range range-lines)
            (mapcar #'parse-integer id-lines)))))

(defun fresh? (fresh-ranges id)
  "Return the first fresh range containing id, or nil if there is no such range.
  Assumes the fresh ranges are sorted to enable early exit."
  (loop for fresh-range in fresh-ranges
        when (<= (car fresh-range) id (cadr fresh-range))
          return fresh-range
        when (> (car fresh-range) id)
          return nil
        finally (return nil)))

(defun range< (range1 range2)
  (destructuring-bind (a1 b1) range1
    (destructuring-bind (a2 b2) range2
      (or (< a1 a2)
          (and (= a1 a2) (< b1 b2))))))

(defun main-1 (filename)
  (destructuring-bind (fresh-ranges . ids) (read-inputs filename)
    (let ((sorted-fresh-ranges (sort fresh-ranges #'range<)))
      (loop for id in ids
            sum (if (fresh? sorted-fresh-ranges id) 1 0)))))

(defun consolidate (fresh-ranges)
  "Remove redundancy in a sorted list of fresh ranges: emit non-overlapping ranges."
  (let ((result nil)
        (current-range (car fresh-ranges)))
    (loop for fresh-range in (cdr fresh-ranges)
          do (if (<= (car fresh-range) (cadr current-range))
                 (setf current-range (list (car current-range)
                                           (max (cadr current-range) (cadr fresh-range))))
                 (progn
                   (setf result (cons current-range result))
                   (setf current-range fresh-range)))
          finally (setf result (cons current-range result)))
    result))

(defun main-2 (filename)
  (destructuring-bind (fresh-ranges . ids) (read-inputs filename)
    (let ((sorted-fresh-ranges (sort fresh-ranges #'range<)))
      (reduce #'+
              (mapcar #'(lambda (range) (1+ (- (cadr range) (car range))))
                      (consolidate sorted-fresh-ranges))))))