advent-of-code-2024

My solutions to AoC 2024
Log | Files | Refs

commit c5b150512827fae8980848c3f48a685db29d3db6
parent 1fa917d31e78f7342ab690a3447ef3ac47c9fcd9
Author: Lukas Henkel <lh@entf.net>
Date:   Thu,  5 Dec 2024 07:21:32 +0100

Split parsing

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

diff --git a/src/day-5.lisp b/src/day-5.lisp @@ -18,22 +18,23 @@ (defun middle-page-number (update) (nth (floor (length update) 2) update)) +(defun process-updates (input rule-map) + (loop for line = (read-line input nil) + until (null line) + for update = (parse-update line) + for sorted = (order-update (copy-seq update) rule-map) + for middle = (middle-page-number sorted) + if (seq= update sorted) + sum middle into task-1 + else + sum middle into task-2 + finally (return (values task-1 task-2)))) + (defun day-5 (input) (loop with rule-map = (make-hash-table :test #'equal) - with processing-updates = nil with task-1 = 0 with task-2 = 0 for line = (read-line input nil) - until (null line) - do (cond - (processing-updates - (let* ((update (parse-update line)) - (sorted (order-update (copy-seq update) rule-map)) - (middle (middle-page-number sorted))) - (if (seq= update sorted) - (incf task-1 middle) - (incf task-2 middle)))) - ((string= line "") - (setf processing-updates t)) - (t (setf (gethash (parse-rule line) rule-map) t))) - finally (return (values task-1 task-2)))) + when (string= line "") + do (return (process-updates input rule-map)) + do (setf (gethash (parse-rule line) rule-map) t)))