Day 06 in Common Lisp optimalization

There's no need to rotate the array itself when you can just rotate an offset
into the array.
This commit is contained in:
aru 2021-12-07 14:09:18 +01:00
parent 16a6302e40
commit f65e1232f6
1 changed files with 3 additions and 6 deletions

View File

@ -27,12 +27,9 @@
(defun solution (data days)
(let ((working (preprocess-input data)))
(dotimes (n days)
(let ((zeroes (aref working 0)))
(loop for x from 1 to 8
do (setf (aref working (1- x)) (aref working x)))
(setf (aref working 8) zeroes)
(setf (aref working 6) (+ zeroes (aref working 6)))))
(dotimes (offset (1+ days))
(setf (aref working (mod (+ offset 6) 9)) (+ (aref working (mod (+ offset 8) 9))
(aref working (mod (+ offset 6) 9)))))
(loop for x from 0 to 8 sum (aref working x))))
(defun part1 (data)