Skip to content

Advent of Code 2021 – Day 1, Puzzle 1

Christmas countdown

Photo by Any Lane from Pexels

I’m participating in the Advent of Code 2021 (@adventofcode). Here’s my solution for Day 1, Puzzle 1 – Measuring depth increases

Problem

The elves have lost the keys to the sleigh into the ocean. Puzzle 1 involves reading depth measurements of the ocean floor to determine how quickly the depth increases. We need to determine from a list of measurements, how many of them are an increase over the previous measurement. We’re given an input list of 2000 measurements to be processed in order.

Solution

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

It’s a fairly straightforward solution of loading the input lines and then checking each row to see if it’s larger than the row before it.

Console.WriteLine("Advent of Code 2021");
Console.WriteLine("Day 1 - Sonar Sweep");
//Solution logic goes here
//Load input array
string[] lines = System.IO.File.ReadAllLines("input.txt");
var depth = int.Parse(lines[0]);
var increases = 0;
for (int i = 1; i < lines.Length; i++)
{
    var measurement = int.Parse(lines[i]);
    if (measurement > depth) increases++;
    depth = measurement;
}
//Write out the solution
Console.WriteLine($"Number of increases: {increases}");

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

The answer is 1624

Leave a Reply