commit db0986abd6d2a985fff0dd0baa13c97a708a6b53 parent c7c064a8bf3e2b6aaaa8c6dc5e00d0553fc20728 Author: Lukas Henkel <lh@entf.net> Date: Fri, 6 Dec 2024 08:04:56 +0100 Slight optimization Check only the spots in task 2 where the guard visited in task 1 Diffstat:
M | src/day-6.lisp | | | 27 | +++++++++++++-------------- |
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/src/day-6.lisp b/src/day-6.lisp @@ -49,23 +49,22 @@ next (next pos dir)) finally (return next))) (push pos visited) - finally (return visited))) + finally (return (remove-duplicates (cdr visited) :test #'equal)))) -(defun task-2 (map initial-pos) +(defun task-2 (map initial-pos visited) (loop with task-2 = 0 - for y from 0 below (input-map-height map) - do (loop for x from 0 below (input-map-width map) - for point = (cons x y) - for cell = (map-cell map point) - when (char= cell #\.) - do (setf (map-cell map point) #\#) - (when (eq (walk-map map initial-pos) :loop) - (incf task-2)) - (setf (map-cell map point) #\.)) + for point in visited + for cell = (map-cell map point) + when (char= cell #\.) + do (setf (map-cell map point) #\#) + (when (eq (walk-map map initial-pos) :loop) + (incf task-2)) + (setf (map-cell map point) #\.) finally (return task-2))) (defun day-6 (input) (let* ((map (make-map input)) - (pos (find-guard map))) - (values (1- (length (remove-duplicates (walk-map map pos) :test #'equal))) - (task-2 map pos)))) + (pos (find-guard map)) + (visited (walk-map map pos))) + (values (length visited) + (task-2 map pos visited))))