Spyke

Replies

Comment on

🧑 - 2025 DAY 2 SOLUTIONS - 🧑

An ID is invalid if and only if it is divisible by a number of the form 1001001001, where the 1's are separated by the same number of 0's and the block length times the number of blocks equals the digit length of the ID. Given that, the problem reduces to summing the members of some arithmetic progressions; we never have to iterate over the members of a range at all.

(ql:quickload :str)

(defun parse-range (range)
  (mapcar #'parse-integer (str:split "-" range)))

(defun parse-line (line)
  (mapcar #'parse-range (str:split "," line)))

(defun read-inputs (filename)
  (let ((input-lines (uiop:read-file-lines filename)))
    (parse-line (car input-lines))))

(defun split-range (start end)
  "Split the range (start end) into a list of ranges whose bounds have same number of digits."
  (let ((start-digits (1+ (floor (log start 10))))
        (end-digits (1+ (floor (log end 10)))))
    (if (< start-digits end-digits)
        (cons (list start (1- (expt 10 start-digits)))
              (split-range (expt 10 start-digits) end))
        (list (list start end)))))

(defun sum-multiples-in-range (d start end)
  "Add up the sum of all multiples n of d satisfying start <= n <= end."
  (multiple-value-bind (q0 r0) (floor start d)
    ;; q1, q2 are coefficients of the least and greatest multiple of d potentially in range
    (let ((q1 (if (zerop r0) q0 (1+ q0)))
          (q2 (floor end d)))
      (if (> q1 q2)
          0
          (flet ((arith-up-to (n) (floor (* n (1+ n)) 2)))
            (* d (- (arith-up-to q2) (arith-up-to (1- q1)))))))))

(defun sum-invalid-in-range (range repeat-count)
  "Add up the sum of all IDs in range start <= n <= end which are invalid due to having
  exactly repeat-count repeats."
  (loop for homogeneous-range in (apply #'split-range range)
        sum (destructuring-bind (hstart hend) homogeneous-range
              (let ((digits (1+ (floor (log hstart 10)))))
                (if (not (zerop (mod digits repeat-count)))
                    0
                    (let ((divisor
                            (loop for k from 0 to (1- digits) by (floor digits repeat-count)
                                  sum (expt 10 k))))
                      (sum-multiples-in-range divisor hstart hend)))))))

(defun main-1 (filename)
  (reduce #'+ (mapcar #'(lambda (range) (sum-invalid-in-range range 2))
                      (read-inputs filename))))

(defun sum-all-invalids-in-range (range)
  "Add up the sum of _all_ invalid IDs (with any available repeat count) in range."
  ;; Composite repeat counts will be overcounted. Because the maximum digit length of
  ;; inputs is limited, we can cheat and just use an explicit constant for weights.
  (let ((repeat-weights '((2 1) (3 1) (5 1) (6 -1) (7 1) (10 -1))))
    (loop for repeat-weight in repeat-weights
          sum (destructuring-bind (repeat-count weight) repeat-weight
                (* weight (sum-invalid-in-range range repeat-count))))))

(defun main-2 (filename)
  (reduce #'+ (mapcar #'sum-all-invalids-in-range (read-inputs filename))))

Comment on

💃 - 2025 DAY 3 SOLUTIONS - 💃

This problem was quite straightforward; I think it probably could have been day 1. The only interesting thing here is the use of values and multiple-value-bind, which are the standard way in Common Lisp to give a function a primary return value but also one or more advisory return values. This lets you return a rich data structure for consumers who want it, but consumers who don't want it don't need to parse that entire data structure to get the primary return value.

(defun parse-line (line)
  (let ((digits (loop for i from 0 to (1- (length line))
                      collect (parse-integer line :start i :end (1+ i)))))
    (make-array (list (length digits)) :initial-contents digits)))

(defun read-inputs (filename)
  (let* ((input-lines (uiop:read-file-lines filename)))
    (mapcar #'parse-line input-lines)))

(defun arg-max (v &key start end)
  "Yield the index and the greatest element of vector v, between indices start (inclusive) and
  end (exclusive) if they are supplied. Returns the earliest instance of the maximum."
  (let* ((start (max 0 (or start 0)))
         (end (min (length v) (or end (length v))))
         (arg-max start)
         (val-max (aref v start)))
    (loop for i from start to (1- end)
          if (> (aref v i) val-max)
            do (progn (setf arg-max i)
                      (setf val-max (aref v i)))
          finally (return (values arg-max val-max)))))

(defun bank-max-joltage (digits bank)
  (let ((search-start 0)
        (result 0))
    (loop for d from 0 to (1- digits)
          do (multiple-value-bind (digit-pos digit)
                 (arg-max bank :start search-start :end (+ (length bank) (- digits) (1+ d)))
               (setf search-start (1+ digit-pos))
               (setf result (+ (* 10 result) digit))))
    result))

(defun main-1 (filename)
  (reduce #'+ (mapcar #'(lambda (bank) (bank-max-joltage 2 bank))
                      (read-inputs filename))))

(defun main-2 (filename)
  (reduce #'+ (mapcar #'(lambda (bank) (bank-max-joltage 12 bank))
                      (read-inputs filename))))

You reached the end