diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-12-14 10:47:00 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-12-14 10:47:53 +0100 |
commit | c427a1fcacfe4e05275acc02971bb00eb7037058 (patch) | |
tree | 5fbc224165219ea9a9cb59a451e7c1b62cb0a581 /AoC2023 | |
parent | d0bd893f59a12fd3b58eaf998a2fc48e7b90e975 (diff) | |
download | scratch-c427a1fcacfe4e05275acc02971bb00eb7037058.tar.gz scratch-c427a1fcacfe4e05275acc02971bb00eb7037058.tar.bz2 scratch-c427a1fcacfe4e05275acc02971bb00eb7037058.zip |
part 2, off by one error
Diffstat (limited to 'AoC2023')
-rw-r--r-- | AoC2023/day11/solver.lisp | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/AoC2023/day11/solver.lisp b/AoC2023/day11/solver.lisp index e7feabf..666b71c 100644 --- a/AoC2023/day11/solver.lisp +++ b/AoC2023/day11/solver.lisp @@ -1,6 +1,7 @@ ;; 9:59 ;; 10:31 part1 -;; +;; 10:47 part2 + (ql:quickload '(fiveam)) (defparameter eg-input "...#...... .......#.. @@ -20,16 +21,16 @@ (defun shift (coord spacers) (count-if (lambda (idx) (< idx coord)) spacers)) -(defun expanded (point free-rows free-cols) +(defun expanded (point free-rows free-cols multiplier) (destructuring-bind (y x) point - (list (+ y (shift y free-rows)) - (+ x (shift x free-cols))))) + (list (+ y (* multiplier (shift y free-rows))) + (+ x (* multiplier (shift x free-cols)))))) (fiveam:test parts (fiveam:is (= 1 (shift 5 '(3 6)))) - (fiveam:is (equal '(7 4) (expanded '(5 3) '(1 2) '(2))))) + (fiveam:is (equal '(7 4) (expanded '(5 3) '(1 2) '(2) 1)))) -(defun expanded-galaxies (rows) +(defun expanded-galaxies (rows &optional (multiplier 1)) (let* ((free-rows (make-array (length rows) :initial-element t)) (free-cols (make-array (length (car rows)) :initial-element t)) (galaxies (loop for row in rows @@ -45,7 +46,8 @@ (lambda (galaxie) (expanded galaxie (indexes free-rows) - (indexes free-cols))) + (indexes free-cols) + multiplier)) galaxies))) @@ -54,13 +56,16 @@ (+ (abs (- bx sx)) (abs (- by sy))))) -(defun solver1 (rows) - (loop with gals = (expanded-galaxies rows) +(defun solver (rows &optional (multiplier 1)) + (loop with gals = (expanded-galaxies rows multiplier) for g across gals for i from 0 sum (loop for j from (1+ i) below (length gals) sum (manhattan-dist g (aref gals j))))) (fiveam:test solutions - (fiveam:is (= 374 (solver1 (uiop:split-string eg-input :separator '(#\Newline))))) - (fiveam:is (= 10154062 (solver1 (uiop:read-file-lines "input"))))) + (fiveam:is (= 374 (solver (uiop:split-string eg-input :separator '(#\Newline))))) + (fiveam:is (= 1030 (solver (uiop:split-string eg-input :separator '(#\Newline)) 9))) + (fiveam:is (= 8410 (solver (uiop:split-string eg-input :separator '(#\Newline)) 99))) + (fiveam:is (= 10154062 (solver (uiop:read-file-lines "input")))) + (fiveam:is (= 553083047914 (solver (uiop:read-file-lines "input") 999999)))) |