diff options
Diffstat (limited to 'AoC2023')
-rw-r--r-- | AoC2023/day16/solver.lisp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/AoC2023/day16/solver.lisp b/AoC2023/day16/solver.lisp index 8b967a4..72abf43 100644 --- a/AoC2023/day16/solver.lisp +++ b/AoC2023/day16/solver.lisp @@ -1,7 +1,7 @@ ;;8:57 ;;10:26 part1 +;;10:53 part2 (ql:quickload '(fiveam arrows alexandria)) -(defparameter eg-in "") (defun in-bounds (field ray) (destructuring-bind (maxrow maxcol) (array-dimensions field) @@ -71,23 +71,37 @@ (remove-if-not (alexandria:curry #'in-bounds field)) (remove-if (alexandria:curry #'energized-p energized))))) -(defun advancer (field rays energized maxi) +(defun advancer (field rays energized) (loop for (dir row col) in rays do (push dir (gethash (cons row col) energized))) - (if (or (null rays) (> maxi 5000)) - energized - (let ((next-moves (mapcan (lambda (ray) (action field ray energized)) rays))) - ;; (format t "~d Next: ~a~%" maxi next-moves) - (advancer field next-moves energized (1+ maxi))))) + (if (null rays) + energized + (let ((next-moves (mapcan (lambda (ray) (action field ray energized)) rays))) + ;; (format t "~d Next: ~a~%" maxi next-moves) + (advancer field next-moves energized)))) -(defun solver1 (input) +(defun boundary-rays (field) + (destructuring-bind (maxrow maxcol) (array-dimensions field) + (append + (loop for i below maxrow collect (list 'rt i 0)) + (loop for i below maxrow collect (list 'lf i (1- maxcol))) + (loop for i below maxcol collect (list 'dw 0 i)) + (loop for i below maxcol collect (list 'up (1- maxrow) i))))) + +(defun solver (field) + (lambda (start) + (hash-table-count + (advancer field (list start) + (make-hash-table :test #'equal))))) + +(defun solution (input &optional (starter (constantly (list (list 'rt 0 0))))) (let* ((rows (length input)) - (energized (make-hash-table :test #'equal)) (field (make-array (list rows (length (car input))) :initial-contents input))) - (advancer field (list (list 'rt 0 0)) energized 0) - (hash-table-count energized))) + (reduce #'max (funcall starter field) :key (solver field)))) (fiveam:test solutions - (fiveam:is (= 46 (solver1 (uiop:read-file-lines "eg-in")))) - (fiveam:is (= 7996 (solver1 (uiop:read-file-lines "input"))))) + (fiveam:is (= 46 (solution (uiop:read-file-lines "eg-in")))) + (fiveam:is (= 7996 (solution (uiop:read-file-lines "input")))) + (fiveam:is (= 51 (solution (uiop:read-file-lines "eg-in") #'boundary-rays))) + (fiveam:is (= 8239 (solution (uiop:read-file-lines "input") #'boundary-rays)))) |