~shunter/advent2021

d063d69ab0c5d83ad5d3c5adec9857d385fc99a2 — Samuel Hunter 2 years ago 7638698
Solve Day 06
2 files changed, 46 insertions(+), 0 deletions(-)

A day06.lisp
A input/day06.txt
A day06.lisp => day06.lisp +44 -0
@@ 0,0 1,44 @@
(defpackage #:advent2021.day06
  (:use #:cl #:alexandria #:cl-ppcre #:advent2021.util)
  (:export #:solve-part-1 #:solve-part-2))

(in-package #:advent2021.day06)



(defparameter +input+
  ;; array of # of fish in each state
  (first (parse-lines
           (lambda (line)
             (loop :with fishes := (make-array 9)
                   :for n :in (mapcar #'parse-integer (split "," line))
                   :do (incf (aref fishes n))
                   :finally (return fishes))))))

(defun tick-fish (fish)
  (rotatef (aref fish 0)
           (aref fish 1)
           (aref fish 2)
           (aref fish 3)
           (aref fish 4)
           (aref fish 5)
           (aref fish 6)
           (aref fish 7)
           (aref fish 8))
  (incf (aref fish 6)
        (aref fish 8)))

(defun solve (generations)
  (let ((fish (copy-array +input+)))
    (dotimes (i generations)
      (tick-fish fish))

    ;; return total fish
    (loop :for n :across fish
          :sum n)))

(defun solve-part-1 ()
  (solve 80))

(defun solve-part-2 ()
  (solve 256))

A input/day06.txt => input/day06.txt +2 -0
@@ 0,0 1,2 @@
1,3,4,1,5,2,1,1,1,1,5,1,5,1,1,1,1,3,1,1,1,1,1,1,1,2,1,5,1,1,1,1,1,4,4,1,1,4,1,1,2,3,1,5,1,4,1,2,4,1,1,1,1,1,1,1,1,2,5,3,3,5,1,1,1,1,4,1,1,3,1,1,1,2,3,4,1,1,5,1,1,1,1,1,2,1,3,1,3,1,2,5,1,1,1,1,5,1,5,5,1,1,1,1,3,4,4,4,1,5,1,1,4,4,1,1,1,1,3,1,1,1,1,1,1,3,2,1,4,1,1,4,1,5,5,1,2,2,1,5,4,2,1,1,5,1,5,1,3,1,1,1,1,1,4,1,2,1,1,5,1,1,4,1,4,5,3,5,5,1,2,1,1,1,1,1,3,5,1,2,1,2,1,3,1,1,1,1,1,4,5,4,1,3,3,1,1,1,1,1,1,1,1,1,5,1,1,1,5,1,1,4,1,5,2,4,1,1,1,2,1,1,4,4,1,2,1,1,1,1,5,3,1,1,1,1,4,1,4,1,1,1,1,1,1,3,1,1,2,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,2,5,1,2,1,1,1,1,1,1,1,1,1