Skip to content

Commit

Permalink
2023/09 refactoring
Browse files Browse the repository at this point in the history
- Create PREDICTION-SEQUENCES, with the sole responsibility of
  generating all the delta sequences for a given value history
- Add EXTRAPOLATE and EXTRAPOLATE-BACKWARD function to codify
  logic for part 1 and part 2 respectively
  • Loading branch information
iamFIREcracker committed Dec 9, 2023
1 parent 31fc38d commit c01f1de
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/2023/day09.lisp
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
(defpackage :aoc/2023/09 #.cl-user::*aoc-use*)
(in-package :aoc/2023/09)

'(&optional (strings (uiop:read-file-lines #P"src/2023/day09.txt")))

(defun parse-input (&optional (strings (uiop:read-file-lines #P"src/2023/day09.txt")))
(mapcar #'extract-integers strings))


(defun deltas (nums)
(loop for n in nums by #'cdr for m in (cdr nums) by #'cdr
collect (- m n)))

(defun extrapolate (nums)
(defun prediction-sequences (history)
(looping
(while (some (complement #'zerop) history)
(collect! history)
(setf history (deltas history)))))


(defun extrapolate (history)
(looping
(while (some (complement #'zerop) nums)
(sum! (car (last nums)))
(setf nums (deltas nums)))))
; (mapcar #'extract-integers (uiop:read-file-lines #P"src/2023/day09.txt"))
; (mapcar #'extrapolate *)
; (reduce #'+ *)
; 1930746032

(defun extrapolate-2 (nums)
(bnd1 (dd (looping
(while (some (complement #'zerop) nums)
(collect! (first nums))
(setf nums (deltas nums)))))
(bnd1 (num 0)
(dolist (d (reverse dd) num)
(setf num (- d num))))))
; (mapcar #'extract-integers (uiop:read-file-lines #P"src/2023/day09.txt"))
; (mapcar #'extrapolate-2 *)
; (reduce #'+ *)
(dolist (nums (prediction-sequences history))
(sum! (car (last nums))))))


(defun extrapolate-backward (history)
(bnd1 (res 0)
(dolist (nums (reverse (prediction-sequences history)) res)
(setf res (- (first nums) res)))))


(define-solution (2023 09) (input parse-input)
(values (reduce #'+ input :key #'extrapolate)
(reduce #'+ input :key #'extrapolate-backward)))

(define-test (2023 09) (1930746032 1154))

0 comments on commit c01f1de

Please sign in to comment.