AoC 2021 - Day 6
Today’s challenge is here. We have to find the amount of fish ‘split’ after some days. A problem based on exponential growth.
When I started doing this challenge I first thought about using Object-Oriented Programming, but that didn’t work out. When calculating large amounts of fish I started to run out of memory and the algorithm was too slow.
Loading the data:
Task 1
Pretty simple, we just need to advance the fish and then count the amount of fish. I created simple methods in the LanternFish
class that do just that.
Here’s a class that represents a fish:
Here we iterate through number of days and advance the fish. The way fishes are related to each other is that they all form a tree. When I advance a fish, I also advance all of its children. The amount of fish is the sum of the amount of fish in the tree.
Task 2
Here I took a different approach, I stored the fishes in a simple dictionary. The key is the day and the value is the number of fishes. I then iterate through the days and advance the fishes.
The way this works is kind of hard to explain really, but the key to it all is the %
modulo operator. The modulo operator returns the remainder of a division. In this case, the modulo operator is used to determine whether some fish should give birth.