2021 Advent of Code: Day 6

Fishes, fishes, oh my! Sooo many fish!

First part not so bad. Brute force it through the 80 days. Just keep making the array bigger with no checks! What could possibly go wrong with that!

        public int LanternfishPart1(List<int> fishes, int NumberOfDays)
        {
            int newFishesToAdd = 0;

            for (int day = 1; day <= NumberOfDays; day++)
            {
                for (int i = 0; i < fishes.Count; i++)
                {
                    switch (fishes[i])
                    {
                        case > 0:
                            fishes[i] = --fishes[i];
                            break;
                        case 0:
                            fishes[i] = 6;
                            newFishesToAdd++;
                            break;
                    }
                }

                for (int i = 0; i < newFishesToAdd; i++)
                {
                    fishes.Add(8);
                }

                newFishesToAdd = 0;
            }

            return fishes.Count();
        }

Second part...well, if you are paying attention, the test case result number is ... well ... a little on the HUGE side for an array. There is a little 2gb limit on object size. Hopefully you did not even bother with the first approach.

Second approach, we need to be smarter about how we are organizing the data. Let's just use an array with 9 elements, each element is the total of 0s, 1s, 2s...8s of the value of the days left for the one fish.

Save off the value of 0 element to add to the 6 element and set the 8 element.

Move down the values. Be sure to zero out!

        public long LanternfishPart2(List<int> StartingFish)
        {
            int stateSize = 9;
            long[] fishState = new long[9] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            foreach(int f in StartingFish)
            {
                fishState[f] += 1;
            }

            long newFishes = 0;
            long fishesToSetTo6 = 0;

            for (int day = 1; day <= 256; day++)
            {

                if (fishState[0] > 0)
                {
                    newFishes = fishesToSetTo6 = fishState[0];
                    fishState[0] = 0;
                }
                if (fishState[1] > 0)
                {
                    fishState[0] = fishState[1];
                    fishState[1] = 0;
                }
                if (fishState[2] > 0)
                {
                    fishState[1] = fishState[2];
                    fishState[2] = 0;
                }
                if (fishState[3] > 0)
                {
                    fishState[2] = fishState[3];
                    fishState[3] = 0;
                }
                if (fishState[4] > 0)
                {
                    fishState[3] = fishState[4];
                    fishState[4] = 0;
                }
                if (fishState[5] > 0)
                {
                    fishState[4] = fishState[5];
                    fishState[5] = 0;
                }
                if (fishState[6] > 0)
                {
                    fishState[5] = fishState[6];
                    fishState[6] = 0;
                }
                if (fishState[7] > 0)
                {
                    fishState[6] = fishState[7];
                    fishState[7] = 0;
                }
                if (fishState[8] > 0)
                {
                    fishState[7] = fishState[8];
                    fishState[8] = 0;
                }

                fishState[6] += fishesToSetTo6;
                fishState[8] = newFishes;

                newFishes = 0;
                fishesToSetTo6 = 0;

            }

            return fishState.Sum();
        }