diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-14 14:39:10 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-14 14:39:10 +0100 |
commit | d2f7f38ab66c7a112f6cabd0be3db27ff4acb78c (patch) | |
tree | 5e7bc6b722515cee7aa621e2a3367a613de9397e | |
parent | 749b857d009dbe4dbfc6ce798f76b0bcd5803b9d (diff) |
Make code a bit more efficient
-rw-r--r-- | 2024/14/puzzles.lisp | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/2024/14/puzzles.lisp b/2024/14/puzzles.lisp index 68f06a1..82144f3 100644 --- a/2024/14/puzzles.lisp +++ b/2024/14/puzzles.lisp @@ -7,17 +7,6 @@ (defconstant +width+ 101) (defconstant +height+ 103) -(defconstant +quadrant-bounds+ - (make-array '(4 4) :initial-contents - (let* ((x +width+) - (y +height+) - (x/2 (floor x 2)) - (y/2 (floor y 2))) - `((0 0 ,x/2 ,y/2) - (,(1+ x/2) 0 ,x ,y/2) - (0 ,(1+ y/2) ,x/2 ,y) - (,(1+ x/2) ,(1+ y/2) ,x ,y))))) - (defparameter *chart* (make-array (list +height+ +width+))) @@ -37,11 +26,13 @@ ;; END PART 1 START PART 2 (handler-case (sb-posix:mkdir "charts" #o755) (sb-posix:syscall-error ())) ; EEXIST - (loop for i from 0 below 10000 - do (setq *chart* (make-array (list +height+ +width+))) - (loop for robot in robots - do (plot-on-chart (location-after-n-seconds i robot)) - finally (save-chart-to-bmp i))) + (dotimes (i 10000) + (dotimes (x +width+) + (dotimes (y +height+) + (setf (aref *chart* y x) 0))) + (loop for robot in robots + do (plot-on-chart (location-after-n-seconds i robot)) + finally (save-chart-to-bmp i))) ;; END PART 2 )) @@ -76,6 +67,18 @@ (defun plot-on-chart (position) (incf (aref *chart* (cdr position) (car position)))) +;; START PART 1 +(defconstant +quadrant-bounds+ + (make-array '(4 4) :initial-contents + (let* ((x +width+) + (y +height+) + (x/2 (floor x 2)) + (y/2 (floor y 2))) + `((0 0 ,x/2 ,y/2) + (,(1+ x/2) 0 ,x ,y/2) + (0 ,(1+ y/2) ,x/2 ,y) + (,(1+ x/2) ,(1+ y/2) ,x ,y))))) + (defun sum-for-quadrant (quadrant) (loop with q1 = (aref +quadrant-bounds+ quadrant 0) with q2 = (aref +quadrant-bounds+ quadrant 1) @@ -84,6 +87,7 @@ for x from q1 below q3 sum (loop for y from q2 below q4 sum (aref *chart* y x)))) +;; END PART 1 (defun integer-char-p (char) (or (digit-char-p char) (char= char #\-))) |