Advent of Code '23 - day 9

Table of Contents

My elisp skill are growing. Writing code goes quicker every time. The first part took a bit longer as I added the wrong results, but it didn't take very long to get there. For part two I only had to add a single reverse.

Input

Example

0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45

Part 1

(defun aoc23/parse-input (string)
  (let ((lines (string-split (string-trim string) "\n")))
    (mapcar (lambda (line)
              (mapcar 'string-to-number
                      (string-split line
                                    " +")))
            lines)))


(defun aoc23/find-next (line &optional carry)
  (let* ((n (car line))
         (newline (mapcar (lambda (m)
                            (let ((q (- m n)))
                              (setq n m)
                              q))
                          (cdr line))))
    (if (apply '= newline)
        (+ n (car newline))
      (+ n
         (aoc23/find-next newline)))))


(apply '+ (mapcar 'aoc23/find-next      
                  (aoc23/parse-input input)))

Part 2

(defun aoc23/parse-input (string)
  (let ((lines (string-split (string-trim string) "\n")))
    (mapcar (lambda (line)
              (reverse (mapcar 'string-to-number
                               (string-split line
                                             " +"))))
            lines)))


(defun aoc23/find-next (line &optional carry)
  (let* ((n (car line))
         (newline (mapcar (lambda (m)
                            (let ((q (- m n)))
                              (setq n m)
                              q))
                          (cdr line))))
    (if (apply '= newline)
        (+ n (car newline))
      (+ n
         (aoc23/find-next newline)))))


(apply '+ (mapcar 'aoc23/find-next      
                  (aoc23/parse-input input)))