Advent of Code 2020 Day 1 - Solution + Tutorial (TypeScript)

published
All TypeScript Solutions for Advent of Code 2020

I'll try to update asap. Please bear in a mind that I'll probably lag one or multiple days behind.


Prerequisites

I assume you've put your puzzle input into an array called lines where each array item is a line of the input text file. It's up to you to either parse the text file or create an array by hand.

const lines = [
  "1946",
  "1800",
  "1911",];

Solution

Puzzle

Just to make sure, you know what I'm talking about, take a look at today's puzzle:

Day 1: Report Repair

Part 1

So given a list of numbers we should find the two entries that sum to 2020. The product of these two entries is the solution to this puzzle.

What are we gonna do? Well first note that lines right now is an array that contains strings only. We'd like to use mathematical operations so let's convert the array to a new array containing only numbers. We can make use of the parseInt function to parse each line and convert the strings to numbers.

const entries = lines.map((line) => parseInt(line));

This leaves us with an array entries that contains many numbers. Now all we have to do is check whether any addition of two numbers results in 2020. Therefore, we could simply iterate over all entries and then iterate over all of them again to try each possible combination. Let's just use nested for-loops for this exercise.

for (const a of entries) {
  for (const b of entries) {
    if (a + b === 2020) {
      return a * b;
    }
  }
}

As soon as any addition of a and b result in 2020 we have found a match. The puzzle asks us to find the product of a and b so we multiply them and return the result. Here I used an early return because the code can stop as soon as we've found a match. There is no reason to keep on iterating.

For completeness - here's the full solution:

const entries = lines.map((line) => parseInt(line));

for (const a of entries) {
  for (const b of entries) {
    if (a + b === 2020) {
      return a * b;
    }
  }
}

Part 2

After we've already solved part 1 of this puzzle, part 2 should be easy. Instead of looking for two numbers, we are looking for three numbers that add to 2020. We can reuse our previous implementation and just add one more loop. So we check each combination with three numbers until we find a match.

for (const a of entries) {
  for (const b of entries) {
    for (const c of entries) {
      if (a + b + c === 2020) {
        return a * b * c;
      }
    }
  }
}

Like in part 1, we can stop as soon as a, b and c add up to 2020. Then we just have to return the product and we are done. Nice job!

Again - here's the full solution:

const entries = lines.map((line) => parseInt(line));

for (const a of entries) {
  for (const b of entries) {
    for (const c of entries) {
      if (a + b + c === 2020) {
        return a * b * c;
      }
    }
  }
}

Conclusion

Day 1 could be considered a warm-up exercise. Two or three simple loops should be enough to solve this puzzle. The time complexity could be optimized, but in my opionion this is nothing to worry about. If you want a "harder challenge" you can always try to be more efficient.

Thanks a lot for reading this post. Please consider sharing it with your friends and colleagues. See you tomorrow!


If you like my content and you want to see more, please follow me on Twitter!
Questions, feedback or just wanna chat? Come and join my Discord!

You May Also Be Interested in the Following Posts

All TypeScript Solutions for Advent of Code 2020

I'll try to update asap. Please bear in a mind that I'll probably lag one or multiple days behind.