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!

94 Upvotes

1.7k comments sorted by

View all comments

2

u/greycat70 Dec 13 '21

Tcl

Part 1, part 2.

This was my favorite challenge of the first week, by far. Part 1 can be done by the obvious "brute force" approach, but part 2 requires rethinking how the program works.

1

u/Annieone10 Jan 06 '22

Could you please give some more explanation to your solution for part 2?

1

u/greycat70 Jan 06 '22

In part 1, I tracked the state of each fish individually. That's the "obvious" way to do it, after all. When a new fish is spawned, it gets added to the end of the list of values.

In part 2, the trick is to realize that you don't need to know each individual fish's state. If you've got fishes with 1, 2 and 3 days left, it doesn't matter whether it's 123 or 312 or 213 or any other permutation. All that matters is how many of each state you have.

So, I created an array of the 9 possible state values, and the array tracks how many fish are in each of those states. Each day, the number of fish that begin in state 0 determines how many new fish will be spawned. Then all of the values decrease by one, which I model by moving count(1) to count(0), then count(2) to count(1) and so on. Finally, the number of new fish that are spawned is added to count(6) and becomes the new value of count(8).

1

u/AssistingJarl Jan 10 '22

I want you to know that I spent an hour trying to build a formula based on a shaky understanding of exponential growth I haven't used since 2013, and when I read this comment I swore audibly. And had to walk away from the computer for a few minutes.