aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2023
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2023')
-rw-r--r--AoC2023/day09/solver.lisp26
1 files changed, 10 insertions, 16 deletions
diff --git a/AoC2023/day09/solver.lisp b/AoC2023/day09/solver.lisp
index 3df0cc3..647284f 100644
--- a/AoC2023/day09/solver.lisp
+++ b/AoC2023/day09/solver.lisp
@@ -13,32 +13,26 @@
(defun terminal-p (row)
(every #'= (cdr row) row))
-(defun get-coefs (row pick &optional acc)
+(defun get-coefs (row &optional (acc 0))
(if (terminal-p row)
- (nreverse (cons (funcall pick row) acc))
- (get-coefs (row-reducer row) pick (cons (funcall pick row) acc))))
+ (+ (car (last row)) acc)
+ (get-coefs (row-reducer row) (+ (car (last row)) acc))))
(defun parse-line (line)
(mapcar #'parse-integer (uiop:split-string line)))
-(defun forward (line)
- (reduce #'+ (get-coefs line (lambda (a) (car (last a))))))
-
-(defun backward (line)
- (reduce #'- (get-coefs line #'car) :from-end t))
-
-(defun solver (lines key)
+(defun solver (lines order)
(reduce #'+
- (mapcar #'parse-line lines)
- :key key))
+ lines
+ :key (alexandria:compose #'get-coefs order #'parse-line)))
(fiveam:test solutions
(fiveam:is (= 114 (solver (uiop:split-string eg-input :separator '(#\Newline))
- #'forward)))
+ #'identity)))
(fiveam:is (= 2105961943 (solver (uiop:read-file-lines "input")
- #'forward)))
+ #'identity)))
(fiveam:is (= 2 (solver (uiop:split-string eg-input :separator '(#\Newline))
- #'backward)))
+ #'nreverse)))
(fiveam:is (= 1019 (solver (uiop:read-file-lines "input")
- #'backward))))
+ #'nreverse))))