Commit Diff


commit - 1fa917d31e78f7342ab690a3447ef3ac47c9fcd9
commit + c5b150512827fae8980848c3f48a685db29d3db6
blob - 3f2e32ac03000d00d324942cefd318799af44ddc
blob + 027c78a4f3741b838b8e9b818d15912b95cdb007
--- src/day-5.lisp
+++ 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)))