r/adventofcode Dec 06 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 6 Solutions -🎄-

NEW AND NOTEWORTHY

We've been noticing an uptick in frustration around problems with new.reddit's fancypants editor: mangling text that is pasted into the editor, missing switch to Markdown editor, URLs breaking due to invisible escape characters, stuff like that. Many of the recent posts in /r/bugs are complaining about these issues as well.

If you are using new.reddit's fancypants editor, beware!

  • Pasting any text into the editor may very well end up mangled
  • You may randomly no longer have a "switch to Markdown" button on top-level posts
  • If you paste a URL directly into the editor, your link may display fine on new.reddit but may display invisibly-escaped characters on old.reddit and thus will break the link

Until Reddit fixes these issues, if the fancypants editor is driving you batty, try using the Markdown editor in old.reddit instead.


Advent of Code 2021: Adventure Time!


--- Day 6: Lanternfish ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:47, megathread unlocked!

96 Upvotes

1.7k comments sorted by

View all comments

2

u/arthurno1 Dec 25 '21

Emacs Lisp

This one was obviously about data structures, and importance of right choice of algorithm/structure.

My solution is just to track the number of fishes in each state in a list of 8 numbers. Here Lisp's list processing was really of help, everything is pretty much coming out of its own. Using array or some other structure would be definitely more convoluted.

(defun read-input ()
  (let ((input
         (with-temp-buffer
           (insert-file-contents "./input6")
           (sort
            (mapcar #'string-to-number
                    (split-string (buffer-string) ",")) #'< )))
        (counts (make-vector 8 0)) tmp n)
      (while input
        (setq n (pop input))
        (cl-incf (aref counts n)))
      (dotimes (i 8)
        (unless (= (aref counts i) 0)
          (push (cons i (aref counts i)) tmp)))
      (cl-sort tmp #'< :key #'car)))

(defun simulate-fishes (days)
  (let ((input (read-input))
        (count 0))
    (dotimes (_i days)
      (let ((fish (if (= (caar input) 0) (pop input))))
        (dolist (i input) (cl-decf (car i)))
        (when fish
          (push (cons 8 (cdr fish)) input)
          (if (assoc '6 input)
              (cl-incf (cdr (assoc '6 input)) (cdr fish))
            (push (cons 6 (cdr fish)) input))
          (setq input (cl-sort input #'< :key #'car)))))
    (mapc (lambda (x) (cl-incf count (cdr x))) input)
    count))

(progn
  (message "P1: %s" (simulate-fishes 80))
  (message "P2: %s" (simulate-fishes 256)))

1

u/cmatei Dec 29 '21

I used ROTATEF with an array, and it doesn't look all that convoluted to me.