DEV Community

Cover image for Advent Of Code 2021 Using C#: Day 01 - Sonar Sweep
Kostas Kalafatis
Kostas Kalafatis

Posted on

Advent Of Code 2021 Using C#: Day 01 - Sonar Sweep

It’s finally December, we’ve been waiting all year. Christmas is fast approaching, and with it the new iteration of Advent of Code.

The Advent of Code is a coding challenge that starts on December 1st and ends on December 25th. Every day a new puzzle is released. To win points (stars) you need to complete the puzzles.

In this series, we are going to discuss each puzzle, and hopefully provide a solution in C#.

Part 1

In Part 1, we need to find how many numbers in the list are greater than the previous number.

So, let's start building our solution. We will first create an algorithm that solves our puzzle, and then identify elements to convert to LINQ queries.

We have an integer array depths containing our depth measurements. Our goal is to find the number of times a depth measurement increases from the previous. For that, we will iterate through every element of the array and compare it with its predecessor. There is a catch here. We need to start our iteration from the second element since there aren't any measurements before the first. Next, if the value of the current is greater than the value of the previous element, we will increase our counter by one.

Below is the pseudocode for the algorithm.

SONAR_SWEEP(DEPTHS)
INPUT: An unordered array of Int32 items
OUTPUT: The number of times an item is greater than its predecessor

Counter = 0

FOR idx = 1 TO LENGTH(DEPTHS) - 1
    IF DEPTHS[idx] > DEPTHS[idx-1]
        Counter += 1
    ENDIF
ENDFOR

RETURN Counter
Enter fullscreen mode Exit fullscreen mode

We have our algorithm fleshed out, so let's implement it using C#.

Below is the implementation.

int counter = 0;
for(int idx = 0; idx < depths.Length; idx++)
{
    if (depths[idx] > depths[idx-1])
        counter +=1;
}

return counter;
Enter fullscreen mode Exit fullscreen mode

This solution works like a charm.

Below is the implementation using LINQ:

int counter = depths.Skip(1)
            .Select((x, i) => x > depths[i])
            .Count(x => x);
Enter fullscreen mode Exit fullscreen mode

Let's break down our query in parts:

We need to start from the second element, so we Skip(1). This will create a new collection. For the sake of clarity we will call this sequence skipped.

We need to Select all elements that are greater than their predecessors. The Select query uses an evaluating function

(x, i) => x > depths[i]
Enter fullscreen mode Exit fullscreen mode

To understand this evaluating function we need take a step back and look at Skip. Skip created a new collection, skipped. In our evaluating function, i is the current index in that skipped collection. Since we skipped one element from the depths, skipped and depths will differ by one element. Thus, x[i] will actually compare to depths[i-1]. The evaluating function will keep only the elements that are greater than their previous.

Finally, we Count the number of elements in our skipped collection to determine the number of depth increases.

Part 2

We are going to solve Part2 in a similar manner. Instead of skipping one element, we are going to skip 3.

int counter = depths.Skip(3)
            .Select((x, i) => x > depths[i])
            .Count(x => x);
Enter fullscreen mode Exit fullscreen mode

That's All Folks!

I hope I could provide y'all with a more in-depth explanation of how my solutions work.

You can find this solution and others in my Github Repo.

Hope you had fun and see you around for the next episode of Advent of Code 2021!

Top comments (0)