fish.lisp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (defparameter *fish-input* "./input.txt")
  2. (ql:quickload :cl-strings)
  3. (use-package :cl-strings)
  4. (defun read-fish ()
  5. "Read input file, returning a list of its fish ages."
  6. (let ((input (open *fish-input* :if-does-not-exist nil))
  7. (fish-ages '()))
  8. (when input
  9. (setq fish-ages (map 'list 'parse-integer (cl-strings:split (read-line input nil) ",")))
  10. (close input))
  11. fish-ages))
  12. (defun process-fish (input-fish)
  13. (defun fish-iter (fish new-fish-ages new-fish-count)
  14. (if (eq fish '())
  15. (append new-fish-ages
  16. (loop for x upto (1- new-fish-count) collect 8))
  17. (let ((current-fish (car fish)))
  18. (cond ((= current-fish 0)
  19. (fish-iter (cdr fish) (cons 6 new-fish-ages) (1+ new-fish-count)))
  20. ('true
  21. (fish-iter (cdr fish) (cons (1- current-fish) new-fish-ages) new-fish-count))))))
  22. (reverse (fish-iter input-fish '() 0)))
  23. (defun process-fish-generations (generations fish)
  24. (defun my-fish-iter (gens fish-ages)
  25. (if (= gens 0)
  26. fish-ages
  27. (my-fish-iter (1- gens) (process-fish fish-ages))))
  28. (my-fish-iter generations fish))
  29. (length (process-fish-generations 80 (read-fish)))
  30. ;; -> 352872
  31. ;; Part 2: 256 generations
  32. ;; my solution is too naive to process this many generations
  33. ;; efficiently :/