From d976ac0b3721ef66a4625458bfe2829c56fa117e Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Thu, 14 Dec 2023 05:56:25 +0100 Subject: more refactoring --- AoC2023/day09/solver.lisp | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'AoC2023/day09/solver.lisp') 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)))) -- cgit v1.2.3