Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021. Here’s my solutions for Day 6 - Lantern Fish.

Problem 1

Puzzles for Day 6 focuses on fish population growth. In this case, fish are on a 7 day cycle. After that cycle, a fish produces a new fish and restarts its own cycle. The new fish has two additional days for its first cycle. So, given this, each fish is represented by a counter: 6 or 8 counting down to 0 each day. After 0, existing fish create a new fish (starting at 8), and reset their own counter to 6. Also, the fish start with a certain number of fish at various points in their own cycles as dictated by the input file.

Solution

All my solutions are written in C#. You can find all my solutions in my Git repo.

This one was quite simple. We start with a list of ints representing our starting pool. Then each day, we reduce the value of each one by 1. And if it is at 0, we add a second fish starting at 8, and reset the current fish to 6. Continue for 80 days.

Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 6 - Puzzle 1");

//Solution logic goes here

//Load input array
string[] lines = System.IO.File.ReadAllLines("input.txt");

var strLanternFish = lines[0].Split(',');
var lanternFish = new List<int>();
foreach (var fish in strLanternFish)
{
    lanternFish.Add(int.Parse(fish));
}

for(int x = 0; x < 80; x++)
{
    var newList = new List<int>();
    foreach (var fishie in lanternFish)
    {
        if(fishie == 0)
        {
            //Time for a new fishie
            newList.Add(8);
            newList.Add(6);
        } else
        {
            //Just another day in the lifecycle
            newList.Add(fishie-1);
        }
    }
    lanternFish = newList;
}

Console.WriteLine($"Total Fish after 80 days: {lanternFish.Count}");

//Stop and wait for enter before exiting
Console.ReadLine();

The answer is 388419

Problem 2

For the second puzzle, instead of 80 days, we continue to 256 days.

Solution

So, my solution above began to struggle under the weight of the lantern fish exponential growth. After 180 or so days, it began to take nearly a minute for each day to process and the List object had well over a trillion elements. And it would get progressively worse with each day being processed.

So, I re-wrote the code to approach it from another angle. Instead of keeping track of each fish individually, and its current lifecycle state, I instead just kept track of the number of fish in each state. And I thought it worked great… at first. It ran in under a second. But it gave me the wrong answer. Took me a few minutes of slow step by step debugging to finally realize that I was using an int datatype and that it was overflowing and rolling back over into negative numbers. Changed it to a ulong and everything worked great.

Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 6 - Puzzle 2");

//Solution logic goes here

//Load input array
string[] lines = System.IO.File.ReadAllLines("input.txt");

var strLanternFish = lines[0].Split(',');
var lanternFish = new List<int>();
foreach (var fish in strLanternFish)
{
    lanternFish.Add(int.Parse(fish));
}

//initialize number of fish
var dictFish = new Dictionary<int, ulong>();
for (int a = 0; a <= 8; a++)
{
    dictFish[a] = 0;
}

foreach (var item in lanternFish)
{
    dictFish[item]++;
}

for (int x = 0; x < 256; x++)
{
    var fishReproducing = dictFish[0];
    for (int y = 0; y < 8; y++)
    {
        //shift all fish down one day
        dictFish[y] = dictFish[y + 1];
    }
    //now reset reproducers
    dictFish[6] = dictFish[6] + fishReproducing;
    dictFish[8] = fishReproducing;
}
ulong fishcount = 0;
for(var b = 0; b < 9; b++)
{
    fishcount += dictFish[b];
}
Console.WriteLine($"Total Fish after 256 days: {fishcount}");

//Stop and wait for enter before exiting
Console.ReadLine();   

The answer is: 1740449478328