src/2021/days/day-06.lisp @ dd27f6b2d778

2021/05 and 2021/06
author Steve Losh <steve@stevelosh.com>
date Mon, 06 Dec 2021 19:38:12 -0800
parents (none)
children b8ca529c9228
(advent:defpackage* :advent/2021/06)
(in-package :advent/2021/06)

(defun parse (stream)
  (mapcar #'parse-integer (str:split #\, (alexandria:read-stream-content-into-string stream))))

(defun simulate (data days)
  (let ((curr (make-array 9 :initial-element 0))
        (next (make-array 9)))
    (dolist (fish data)
      (incf (aref curr fish)))
    (do-repeat days
      (loop :for i :from 8 :downto 0
            :for n = (aref curr i)
            :do (if (zerop i)
                  (progn (setf (aref next 8) n)
                         ;; downto is important to make sure 6 is already by now
                         (incf (aref next 6) n))
                  (setf (aref next (1- i)) n)))
      (rotatef curr next))
    curr))

(define-problem (2021 6) (data parse) (371379 1674303997472)
  (values
    (summation (simulate data 80))
    (summation (simulate data 256))))


#; Scratch --------------------------------------------------------------------