Day 2: Gift Shop

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
    9
    ·
    3 days ago

    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))))