aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-14 05:56:25 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-14 05:56:25 +0100
commitd976ac0b3721ef66a4625458bfe2829c56fa117e (patch)
treec1f35c713a74c9a6a05efcdedc2aeb3357dec8ff
parentdf11dac21fd4d20f5eef5bef2f2826cd69f62c29 (diff)
downloadscratch-d976ac0b3721ef66a4625458bfe2829c56fa117e.tar.gz
scratch-d976ac0b3721ef66a4625458bfe2829c56fa117e.tar.bz2
scratch-d976ac0b3721ef66a4625458bfe2829c56fa117e.zip
more refactoring
-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))))