I've decided, a bit late, to start sharing my solutions to this years Advent of Code. My posted solutions will of course provide the correct answer, but may not always be the optimal solution. Probably never the optimal solution. I usually create a Console Application that prints the answers we're looking for.
For those of you that aren't familiar, Advent of Code is a series of coding challenges of varying difficulty that runs from December 1st until December 25th each year. There's no time limit to complete the challenges, so if you can't do it the day they're posted, it's fine. Just do it later. Each day is a new challenge in 2 parts, and the top 100 quickest solutions to each part gets points for the public leaderboard. But don't worry, the rest of us can still view our personal stats and/or create private leaderboards to compare ourselves with friends. I won't be anywhere near the top 100 during any part of this years AoC.
So, on to the challenge for Day 1. Our input is a list of depth measurements. For part 1, we need to count the number of times when the current depth is larger than the previous depth. This is a fairly simple problem.
The input looks something like this:
199
200
208
210
200
207
240
269
260
263
First, we need to read and parse the input:
var input = (await File.ReadAllLinesAsync("input.txt")).Select(int.Parse).ToArray();
Then, loop through the list and count the number of times the current value is larger than the previous. I use a nullable int, because for the first entry in the list, there is no previous value so it shouldn't be compared with anything and will not count as an increase.
private static void Part1(IEnumerable<int> input)
{
int? previousDepth = null;
var increased = 0;
foreach (var depth in input)
{
if (depth > previousDepth)
{
increased++;
}
previousDepth = depth;
}
Console.WriteLine($"Answer Part 1: {increased}");
}
The second part is a little more tricky, this time we need to compare the sums of windows of 3 depth measurements, and count how many times the sum is larger than the previous one. These are "sliding" windows, meaning that the first sum will be made up of measurements 1, 2 and 3. The second sum will be from measurements 2, 3 and 4, and so on. We should not start comparing until we have an intial value made up of 3 measurements.
private static void Part2(IReadOnlyList<int> input)
{
var queue = new Queue<int>();
queue.Enqueue(input[0]);
queue.Enqueue(input[1]);
queue.Enqueue(input[2]);
var previousDepthSum = queue.Sum();
var increased = 0;
foreach (var depth in input.Skip(3))
{
queue.Dequeue();
queue.Enqueue(depth);
var depthSum = queue.Sum();
if (depthSum > previousDepthSum)
{
increased++;
}
previousDepthSum = depthSum;
}
Console.WriteLine($"Answer Part 2: {increased}");
}
Here I use a queue, which is FIFO (First-In-First-Out), and start by populating it with our first three values and calculate our first depth sum from those three values. Then, it's just a matter of dequeing and enqueing as I loop through the depth measurements, comparing the sum of the current values to the previous sum.