I found this neat little site, big thank you to Eric Wastl, called the Advent of Code. It will provide two coding challenges everyday, with the goal to save my Christmas! There’s a cute story behind it, go check it out. I have to collect 50 stars, 2 per day, 1 per puzzle, to save my Christmas.

I love these little challenges because it keeps me sharp, makes me think about things differently than I may be doing daily at work. Also, who doesn’t love to code?

If you’re starting out, or fairly new to coding, or an old guy like me, I challenge you to give this a try, every day, until Christmas.

Come join me on my journey, if you get stuck, feel free to check out how I did it, and if I get stuck, maybe you can help me too!

Can I do it? I hope so…

### Join my leader board

The site also provides a leaderboard, so I went and created one. I invite you to join my board, and we can see who can win it ;) Go to “Leaderboard” in the top nav and enter `1030369-e174b794` to join my board.

Follow along on github: pretty pages or the repo.

## Spoilers ahead!

I highly encourage you to get through the day’s challenge on your own. I would love to hear how you did though! Did you find a better approach than I did?

### Day 1: Report Repair

#### Part I

We have a page with a long list of numbers. We have to find which two add up to 2020, and then what is their product (the result from multiplying a number by a number).

I opened dev toolbar, and grabbed all of the numbers from the page:

```var nums = window.document.getElementsByTagName("pre")[0]
nums = nums.innerText.split('\n')
```

Now `nums` is an array. Let’s loop through and find the pair!

```nums.forEach(first => {
nums.forEach(second => {
if(Number(first) + Number(second) === 2020) {
console.warn(first, 'plus', second, 'equals 2020')
console.warn(first, 'times', second, 'equals', Number(first) * Number(second))
}
})
})
```

Pretty light weight, here’s what I’m doing:

• Loop through all the numbers using `forEach()`
• Then while I have 1 of the numbers, then I loop through all the numbers again and do the math: does this number plus the second number equal 2020?
• Note the `Number()` method in there, that’s to convert the string to an actual number
• If so, then I log it out (I use `console.warn` so it pops in my console) with the final answer.

We got the answer listed twice in the console! Do you know why?

#### Part II

The second test is very similar, but instead of finding 2 numbers, I need to find 3 numbers, all adding up to 2020, and share their product. I modified the code like so:

```nums.forEach(first => {
nums.forEach(second => {
nums.forEach(third => {
if(Number(first) + Number(second) + Number(third) === 2020) {
console.warn(first, 'plus', second, 'plus', third, 'equals 2020')
console.warn(first, 'times', second, 'times', third, 'equals', Number(first) * Number(second) * Number(third))
}
})
})
})
```

I added a third loop in there. And the answer came

### How did you do?

How did you find the answer on your own? How did you do it? I’d love to hear how you did! Please comment below!

Series Navigation<< Advent of Code, Day 2

## Add yours

1. Complexity Chad says:

bruh at least try to write efficient solutions that’s the challenge

1. define efficient… my goal was to help instruct and code cleanly to help explain the why and how. That’s my efficiency in these posts. Also, I think day 10 is when I really learned some performance techniques, going from a year to a few seconds to process :D