Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
Last active December 6, 2021 19:24
Show Gist options
  • Save commander-trashdin/2dfa607d5adf3456f7596032a14cb198 to your computer and use it in GitHub Desktop.
Save commander-trashdin/2dfa607d5adf3456f7596032a14cb198 to your computer and use it in GitHub Desktop.
AoC 2021, day 6
(defun read-cycle-num (path) ;;reading input
(flet ((convert (char)
(- (char-code char) 48)))
(with-open-file (stream path :direction :input :if-does-not-exist :error)
(let ((res (make-array 0 :adjustable t :fill-pointer 0 :element-type 'fixnum)))
(loop :for cycle := (read-char stream nil nil)
:while cycle
:do (vector-push-extend (convert cycle) res)
(read-char stream nil nil))
res))))
(defun growth (n a0 a1 a2 a3 a4 a5 a6 a7 a8) ;;progression, it's x_n = x_(n-7) + x_(n-9)
(if (< n 9)
(elt (vector a0 a1 a2 a3 a4 a5 a6 a7 a8) n)
(loop :repeat (- n 8) :do (shiftf a0 a1 a2 a3 a4 a5 a6 a7 a8 (+ a0 a2))
:finally (return a8))))
(defun calc-first-9-days (array) ;;need first 9 elements
(loop :repeat 9
:collect (length array)
:do (loop :for i :from 0 :below (length array)
:if (= 0 (aref array i))
:do (vector-push-extend 8 array)
(setf (aref array i) 6)
:else :do (decf (aref array i)))))
(defun day6.1 (path)
(let ((initial (read-cycle-num path)))
(apply #'growth 80 (calc-first-9-days initial))))
(defun day6.2 (path)
(let ((initial (read-cycle-num path)))
(apply #'growth 256 (calc-first-9-days initial))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment