Skip to the content.

Day 6

Return to main page

This was a day where I had a lot of benefit from my participation last year. The exponential growth aspect of the problem immediately signalled to me that I should not try to keep track of individual fish to iterate over, as this would get prohibitively slow. I quickly realized that I just needed to keep track of how many fish were at each stage, resulting in only needing to keep track of 9 values (0 - 8). I again used Numpy to get a fast and efficient solution.

Part 1

The sea floor is getting steeper. Maybe the sleigh keys got carried this way?

A massive school of glowing lanternfish swims past. They must spawn quickly to reach such large numbers - maybe exponentially quickly? You should model their growth rate to be sure.

Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, each lanternfish creates a new lanternfish once every 7 days.

However, this process isn’t necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents the number of days until it creates a new lanternfish.

Furthermore, you reason, a new lanternfish would surely need slightly longer before it’s capable of producing more lanternfish: two more days for its first cycle.

So, suppose you have a lanternfish with an internal timer value of 3:

A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0 is included as a valid timer value). The new lanternfish starts with an internal timer of 8 and does not start counting down until the next day.

Realizing what you’re trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:

3,4,3,1,2

This list means that the first fish has an internal timer of 3, the second fish has an internal timer of 4, and so on until the fifth fish, which has an internal timer of 2. Simulating these fish over several days would proceed as follows:

Initial state: 3,4,3,1,2
After  1 day:  2,3,2,0,1
After  2 days: 1,2,1,6,0,8
After  3 days: 0,1,0,5,6,7,8
After  4 days: 6,0,6,4,5,6,7,8,8
After  5 days: 5,6,5,3,4,5,6,7,7,8
After  6 days: 4,5,4,2,3,4,5,6,6,7
After  7 days: 3,4,3,1,2,3,4,5,5,6
After  8 days: 2,3,2,0,1,2,3,4,4,5
After  9 days: 1,2,1,6,0,1,2,3,3,4,8
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8

Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each other number decreases by 1 if it was present at the start of the day.

In this example, after 18 days, there are a total of 26 fish. After 80 days, there would be a total of 5934.

Find a way to simulate lanternfish. How many lanternfish would there be after 80 days?

The string parsing part was really straightforward, the fish were indicated in a comma-separated string that I convert to a list of int. Subsequently, because I do not need to keep track of the individual fish, but just of the number of fish in each stage I calculate a histogram. Each bin contains the amount of fish for that day.

def _parse_data(self, input_data: str) -> Any:
    fishtogram = np.histogram([int(x) for x in input_data.split(",")], bins=9, range=(0, 9))[0]
    return fishtogram

Once you have the histogram, the solution itself is quite simple. The fish move one stage for each day, which is achieved by using the np.roll function, which shifts each entry and wraps around. This also make sure the newborn fish are added to the end. The only extra step is to also ‘reset’ the fish that gave birth and add them to the reset point (stage 6).

def _solve_part1(self, parsed_data: np.ndarray) -> Any:
    fishies = parsed_data.copy()
    for day in range(80):
        fishies = np.roll(fishies, -1)
        fishies[6] += fishies[-1]
    return np.sum(fishies)

Part 2

Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?

After 256 days in the example above, there would be a total of 26984457539 lanternfish!

How many lanternfish would there be after 256 days?

Exactly the same as the solution for part 1, just need to increase the number of days.

def _solve_part2(self, parsed_data: np.ndarray) -> Any:
    fishies = parsed_data.copy()
    for day in range(256):
        fishies = np.roll(fishies, -1)
        fishies[6] += fishies[-1]
    return np.sum(fishies)

Return to main page