(defparameter *fish-input* "./input.txt") (ql:quickload :cl-strings) (use-package :cl-strings) (defun read-fish () "Read input file, returning a list of its fish ages." (let ((input (open *fish-input* :if-does-not-exist nil)) (fish-ages '())) (when input (setq fish-ages (map 'list 'parse-integer (cl-strings:split (read-line input nil) ","))) (close input)) fish-ages)) (defun process-fish (input-fish) (defun fish-iter (fish new-fish-ages new-fish-count) (if (eq fish '()) (append new-fish-ages (loop for x upto (1- new-fish-count) collect 8)) (let ((current-fish (car fish))) (cond ((= current-fish 0) (fish-iter (cdr fish) (cons 6 new-fish-ages) (1+ new-fish-count))) ('true (fish-iter (cdr fish) (cons (1- current-fish) new-fish-ages) new-fish-count)))))) (reverse (fish-iter input-fish '() 0))) (defun process-fish-generations (generations fish) (defun my-fish-iter (gens fish-ages) (if (= gens 0) fish-ages (my-fish-iter (1- gens) (process-fish fish-ages)))) (my-fish-iter generations fish)) (length (process-fish-generations 80 (read-fish))) ;; -> 352872 ;; Part 2: 256 generations ;; my solution is too naive to process this many generations ;; efficiently :/