Advent of Code - Day 1

3 minute read

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved using any programming language you would like. I’m going to see how far I can make it this year.

Day 1 is broken into two parts. Part 1 starts with us reading in a list of numbers, find the two numbers that, added together, equal 2020, and then multiplying those two numbers together. There is an overarching story to the challenge, but that is the nuts/bolts.

The list is in random order, but key to solving this is to reorder the list into ascending order. Here is the code I used to solve Part 1:

// Step 1: Read in the data and convert to array of integers
// Used example from this site to help: https://www.techiedelight.com/convert-string-array-to-int-array-csharp/
int[] entries = Array.ConvertAll((File.ReadAllLines(@"input.txt")), s => int.Parse(s));

// Step 2: Sort array from lowest to highest
Array.Sort(entries);

// Step 3: Iterate over array 
for (int x = 0; x < entries.Length; x++)
{
    // Because we are sorted, we only have to add to ones that are below us
    for (int y = x + 1; y < entries.Length; y++)
    {
        //Calculate the sum and see if it equals 2020
        int sum = entries[x] + entries[y];
        if (sum == 2020)
        {
            //I have found the two entries that I need. I should save them and break out of the loops
            entry1 = entries[x];
            entry2 = entries[y];
            foundEntries = true;
            break;
        }

    }
    
    if (foundEntries)
        break;
}

// Step 4: Calculate value and output
Console.WriteLine("Final Value For Part 1: " + (entry1 * entry2));

Because I reordered the list in ascending order, I can use a set of two nested loops to iterate over the list. The outer loop goes the length of the list. The inner loop goes from the current position of the outer loop PLUS ONE to the length of the list. Once I find the two numbers that, added together, equal 2020, I save those two numbers, break out of both loops, and output the results.

Part 2 builds off of Part 1, but instead of two numbers that add to 2020, we have to find three numbers that add to 2020 and then multiplying those three numbers together. I’ve already read in the list in Part 1. Here is the code for Part 2:

// PART 2
Log("Start Day01 Run - Part 2");
entry1 = 0;
entry2 = 0;
int entry3 = 0;
foundEntries = false;

// Step 3: Iterate over array as it was read in
for (int x = 0; x < entries.Length; x++)
{
    // Because we are sorted, we only have to add to ones that are below us
    for (int y = x + 1; y < entries.Length; y++)
    {
        for (int z = x + 2; z < entries.Length; z++)
        {
            //Calculate the sum and see if it equals 2020
            int sum = entries[x] + entries[y] + entries[z];
            if (sum == 2020)
            {
                //I have found the two entries that I need. I should save them and break out of the loops
                entry1 = entries[x];
                entry2 = entries[y];
                entry3 = entries[z];
                foundEntries = true;
                break;
            }
        }
        if (foundEntries)
        break;
    }
    
    if (foundEntries)
        break;
}

// Step 4: Calculate value and output
Console.WriteLine("Final Value For Part 1: " + (entry1 * entry2 * entry3));

The solution here is relatively simple, just adding a third nested loop that always starts at the outer loop value PLUS TWO. This works because we only have to move forward through the list, because as we move forward, we will continue to cover all the potential number combinations.

I also added some logging statements, to allow me to help troubleshoot, and because I was interested in how fast things might run. Here are the results from my code:

Overall, it ran pretty fast (which we would expect, the dataset isn’t that large).

You want to see a more elegant solution, go check out Dylan’s solution for Day 1. He has a great video showing how he solved it.

Tags:

Categories:

Updated: