advent-of-code-2023

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

commit d6d76c2348e12e43bf9488048af3427a304053dd
parent 79070780854b628e3aec5095064ead5a73e2971b
Author: Lukas Henkel <lh@entf.net>
Date:   Sat,  9 Dec 2023 06:32:33 +0100

Massively simplify task 2

List can simply be reversed and we can reuse the function from task 1

Diffstat:
Msrc/day-9.lisp | 20+++++---------------
1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/src/day-9.lisp b/src/day-9.lisp @@ -3,30 +3,20 @@ (:export #:day-9)) (in-package #:aoc/day-9) -(defun diff-sequence (numbers) - (loop with last = (first numbers) - for number in (rest numbers) - collect (- number last) - do (setf last number))) - (defun find-next-value (numbers) - (let ((new-sequence (diff-sequence numbers))) + (let ((new-sequence (loop with last = (first numbers) + for number in (rest numbers) + collect (- number last) + do (setf last number)))) (if (every (curry #'= 0) new-sequence) (first numbers) (+ (lastcar numbers) (find-next-value new-sequence))))) -(defun find-previous-value (numbers) - (let ((new-sequence (diff-sequence numbers))) - (if (every (curry #'= 0) new-sequence) - (first numbers) - (- (first numbers) - (find-previous-value new-sequence))))) - (defun day-9 (input) (loop for line = (read-line input nil) while line for numbers = (read-number-list line) sum (find-next-value numbers) into task-1 fixnum - sum (find-previous-value numbers) into task-2 fixnum + sum (find-next-value (nreverse numbers)) into task-2 fixnum finally (return (values task-1 task-2))))