commit db0986abd6d2a985fff0dd0baa13c97a708a6b53 from: Lukas Henkel date: Fri Dec 06 07:04:56 2024 UTC Slight optimization Check only the spots in task 2 where the guard visited in task 1 commit - c7c064a8bf3e2b6aaaa8c6dc5e00d0553fc20728 commit + db0986abd6d2a985fff0dd0baa13c97a708a6b53 blob - cfbc66e5a32a3916228ab874d072a61e7e9b951d blob + 2a21094e0eeba156116e0da9544abf12ddad2d62 --- src/day-6.lisp +++ 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))))