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

  • ystael@beehaw.org
    link
    fedilink
    arrow-up
    2
    ·
    5 days 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))))))