Day 6 is here, and this time it's about fish. We need to calculate how many lanternfish will spawn in a given number of days. There are a few set rules about how the lanternfish spawn:
- Each lanternfish creates a new lanternfish every 7 days
- A newly created lanternfish needs 2 extra days before it starts creating new fish of its own.
- When a lanternfish has created a new fish, it starts a new 7 day cycle that ends with it creating another fish.
Our input will look something like this:
3,4,3,1,2
This represents five different fish, and the number of days until they create a new fish. 0 is also a valid number of days, so it's not until after day 0 a new fish is created and the number is reset.
So in short, after a fish has passed day 0, a new fish is created with a value of 8, and the existing fish is reset to a value of 6. Simple enough, right?
First, we parse our input:
var input = (await File.ReadAllLinesAsync("input.txt"))[0].Split(',').Select(int.Parse).ToArray();
Then, I was lucky enough when solving Part 1, that the solution actually works for Part 2 aswell. Since we don't get to see Part 2 until we've solved Part 1, we never know what the question might be. In this case, Part 1 and Part 2 both want to know how many fish exists after a certain amount of days, and since my solution takes the number of days as a parameter it can be used for both parts. Here's how I did it:
private static long AgeFish(IEnumerable<int> input, int days)
{
var fishCount = new Dictionary<int, long>
{
{0, 0},
{1, 0},
{2, 0},
{3, 0},
{4, 0},
{5, 0},
{6, 0},
{7, 0},
{8, 0}
};
input.GroupBy(x => x).ToList().ForEach(x => fishCount[x.Key] = x.Count());
for (var i = 0; i < days; i++)
{
long? tempVal = null;
foreach (var key in fishCount.Keys.OrderByDescending(x => x))
{
var count = tempVal ?? fishCount[key];
var newKey = key == 0 ? 6 : key - 1;
if (key == 0)
{
fishCount[8] = count;
fishCount[6] += count;
}
else
{
tempVal = fishCount[newKey];
fishCount[newKey] = count;
}
}
}
return fishCount.Values.Sum();
}
First, I create a dictionary to keep track of the amount of fish on each day in the cycle. Then I take the number of existing fish (from our input) and add those to the dictionary. After that we move forward, day by day, and basically just move the fish count down by a step each day, with some special handling if a fish has reached day 0.
Simple enough, and it works for both parts :)
Article Series
Advent Of Code 2021
Advent Of Code 2021 - Day 1
I've decided, a bit late, to start sharing my solutions to this years Advent of Code. My posted sol…
Advent Of Code 2021 - Day 2
Right, on to the next part, Day 2 of Advent of Code. This time, we're trying to work out where our …
Advent Of Code 2021 - Day 4
Time for Day 4 of Advent of Code. Time to play bingo with a giant squid. The question for Part 1 is…
Advent Of Code 2021 - Day 5
Day 5 of Advent of Code was a little bit tricky actually, and I struggled for a bit between Part 1 a…
Advent Of Code 2021 - Day 6
Day 6 is here, and this time it's about fish. We need to calculate how many lanternfish will spawn i…
Advent Of Code 2021 - Day 7
On Day 7 we're being attacked by a giant whale. Luckily, there are a bunch of carbs in tiny submari…
Advent Of Code 2021 - Day 8
Day 8 was a very word-y task, so I'll try to sum it up. If it seems confusing, it's because I was qu…
Advent of Code 2021 - Day 9
This was a tricky one, for me. Day 9 of Advent of Code is probably the one that I've struggled with…
Advent Of Code 2021 - Day 10
Moving on with Day 10 of Advent of Code. This one actually went pretty smooth for me, I solved Part…
Advent Of Code 2021 - Day 11
It's Day 11, time to deal with flashing octopuses. We have found a neatly organized gang of octopus…
Advent Of Code 2021 - Day 12
Day 12 is here, time for recursion again! There are probably some fancy path finding algorithms out…
Advent Of Code 2021 - Day 13
We're passing the half way point today, Day 13. We're gonna celebrate by folding some transparent p…
Advent Of Code 2021 - Day 14
Today was not a good day. I've struggled with the solution for Day 14 , particularly Part 2. We ne…
Advent Of Code 2021 - Day 15
Today ( Day15 ) we are once again navigating through a cave, and we're trying to avoid hitting the w…
Advent Of Code 2021 - Day 16
We're getting closer to christmas, and for me the challenges are getting harder each day. On Day 16…
Advent Of Code 2021 - Day 17
Day 17 was actually alot of fun. We're sending out a probe into a sand trench, to look for our sleig…
Advent Of Code 2021 - Day 18
Hi again, I'm back after some time of during the holidays. I realize it's a bit late to finish this …