-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
31fc38d
commit c01f1de
Showing
1 changed file
with
27 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |