advent-of-code-2023

My solutions to AoC 2023
git clone git://git.entf.net/advent-of-code-2023
Log | Files | Refs

day-9.lisp (1867B)


      1 (defpackage #:aoc/day-9
      2   (:use #:cl #:aoc/utils)
      3   (:export #:day-9))
      4 (in-package #:aoc/day-9)
      5 
      6 (defun day-9 (input)
      7   (loop with task-1 fixnum = 0
      8         with task-2 fixnum = 0
      9         for line = (read-line input nil)
     10         while line
     11         for numbers = (read-number-list line)
     12         do (loop with sequences = (list (cons (car numbers)
     13                                               (lastcar numbers)))
     14                  with current = numbers
     15                  with current-last = 0
     16                  for non-zero? = nil
     17                  do (setf current
     18                           (loop with last fixnum = (first current)
     19                                 for number fixnum in (rest current)
     20                                 for n = (the fixnum (- number last))
     21                                 unless (xor non-zero?
     22                                             (zerop n))
     23                                   do (setf non-zero? t)
     24                                 collect n
     25                                 do (setf last number)
     26                                 finally (setf current-last n)))
     27                  do (push (cons (car current)
     28                                 current-last)
     29                           sequences)
     30                  while non-zero?
     31                  finally (progn
     32                            (loop with next-1 fixnum = 0
     33                                  with next-2 fixnum = 0
     34                                  for current in (rest sequences)
     35                                  do (setf next-1 (+ (the fixnum (cdr current)) next-1)
     36                                           next-2 (- (the fixnum (car current)) next-2))
     37                                  finally (progn
     38                                            (incf task-1 next-1)
     39                                            (incf task-2 next-2)))))
     40         finally (return (values task-1 task-2))))