Created
December 6, 2021 11:49
-
-
Save death/0ae8d127482c5805daf6b13f2d6fb0ec to your computer and use it in GitHub Desktop.
aoc2021-day6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;;; +----------------------------------------------------------------+ | |
| ;;;; | Advent of Code 2021 | | |
| ;;;; +----------------------------------------------------------------+ | |
| (defpackage #:snippets/aoc2021/day6 | |
| (:use #:cl) | |
| (:export | |
| #:day6)) | |
| (in-package #:snippets/aoc2021/day6) | |
| (defconstant creator-tick 6) | |
| (defconstant initial-tick (+ creator-tick 2)) | |
| (defconstant num-ticks (1+ initial-tick)) | |
| (defstruct sim | |
| (counts (make-array num-ticks :initial-element 0)) | |
| (total 0)) | |
| (defun create-sim (input) | |
| (let ((sim (make-sim))) | |
| (dolist (tick input) | |
| (incf (aref (sim-counts sim) tick)) | |
| (incf (sim-total sim))) | |
| sim)) | |
| (defun sim-step (sim) | |
| (let ((creators (aref (sim-counts sim) 0))) | |
| (replace (sim-counts sim) (sim-counts sim) :start2 1) | |
| (setf (aref (sim-counts sim) initial-tick) creators) | |
| (incf (aref (sim-counts sim) creator-tick) creators) | |
| (incf (sim-total sim) creators))) | |
| (defun sim-walk (sim n) | |
| (loop repeat n do (sim-step sim)) | |
| (sim-total sim)) | |
| (defun day6 (input) | |
| (let ((sim (create-sim input))) | |
| (list (sim-walk sim 80) | |
| (sim-walk sim (- 256 80))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment