Advent Of Code 2020 – Day 1

Advent Of Code 2020 – Day 1

Puzzles, Solutions, Challenges

Over the last couple of years, I have been taking part in a super casual coding challenge called Advent Of Code. Each year in December a new daily challenge is published. As a challenger, you’re given input to a puzzle and asked to figure out the programmable solution and derive an answer.

One of the great things about Advent Of Code is that each input is unique to the next person so no one can copy an answer they’ve read elsewhere.

Confession, normally I get a couple of days in and find that one puzzle that completely passes my knowledge. Yet each year I come back for more and I find myself chipping away more and more as I’ve improved over the year. I will eventually get a full year completed.

To make it fair on myself I don’t visit the subreddit for days where I have not completed it. As there are often a lot of solutions and I don’t achieve anything doing this. I’d much prefer to jot down what I think I should be doing, give it an attempt to write. If I am still having issues then I will research the subject, for example, if it’s something I’ve not used before like finding the shortest route. Now I am not a maths person and a lot of it goes over my head but I find research helps me get a better understanding of the solution I want.

This year was no different; I feel I started strong made some mistakes and yet still managed to get a few days in without too many issues.

When competing I often do my challenges with PowerShell. This is twofold, one as it’s my main language of choice and it’s easy to start a file and get a result back. It may not be the most optimal language for the challenges but a solution is a solution.

The downfall came after getting up to day 7. I feel like I had an understanding of what I was required to do but not quite getting the solution. Deciding that taking a step away from the challenge might be the best as it often comes to me when I don’t think about it. Most years I would leave it there I often get swamped with work and have to limit my available time.

This year I’ve found I have a bit more time on my hands than normal. I decided that I wanted to give myself a slightly different challenge. My new goal was to not only complete the challenges in PowerShell but to try my hand at getting a better understanding of a couple of languages.

My new goal with Advent Of Code was to not only find solutions within PowerShell but to now also include C#, JavaScript & Python. I chose these as they are languages that are often used in an area of work I am interested in. I figure anything that gives me exposure to the languages is a good stepping stone. 

So let us partially go through my solution for day 1 and see what I picked up, I won’t be putting my full solution here. If you’d like to see my solutions you are able to view them at 

Day 1 – Part 1

Given the input find two numbers that add up to 2020 with these numbers multiply them.

This at first glance was a simple question given the input file find two numbers that totaled 202 and multiply them together for the solution.

My first attempt was simple, import the content of the input file and loop through the content on each number, then loop it again so we can compare the numbers to make 2020. 

As a first solution it worked as expected, but it was slow much slower than it needed to be. 

I ended up changing my solution to loop through the numbers once, with a given number we would minus if from 2020 and see if the file content contained the number we required. This meant we only looped through the file once instead of one for the first number and a second time to see if the second number would make 2020. 

You can find my first attempt and second attempts at both solutions within my GitHub page, I ran the same comparisons for all languages.

Let’s have a look at the bulk of my answers below where the main elements start


foreach ($i in $content) {
        $tofind = 2020 - [int]$i
        if ($content.Contains($tofind)) {
            $results = [int]$i * [int]$tofind
            Write-Host "Results: $results"


foreach (int item in content)
   int toFind = 2020 - item;
   if (content.Contains(toFind))
       int results = item * toFind;
       return results;


for (let element of array) {
   var toFind = 2020 - parseInt(element)
   if (array.includes(`${parseInt(toFind)}`)) {
      results = parseInt(element) * parseInt(toFind)


for a in lines:
        toFind = 2020 - a
        if toFind in lines:
            resultsa = a * toFind
            end_Time = time.time()
            final_Time = end_Time - start_Time

Essentially we’re looping through each number within the input file and taking it away from 2020 and seeing if the remaining number exists within the input file.

I think it’s only fair to discuss some of the differences I have found within each solution, as on the surface they all look very similar. First looking at importing the files.


This is a simple method which grabs the content of the file and puts it into a nice array, Within PowerShell you don’t need to specify the extra steps as this is already handled.

$content = Get-Content("..\input.txt")


Gives you a couple of extra steps to take the input of a file, you need to initially specify you’re using the System.IO and then you need to specify you’re creating an array from the file. In my case, the content is all numbers so we needed to make the array an Int as well and parse the content to show that we want a number file. There are a few extra steps, but a good lesson in the handling of external content.

using System.IO;

int[] content = Array.ConvertAll(File.ReadAllLines("..\\input.txt"), stringInput => int.Parse(stringInput));


Again we are required to add functionality to our solution by including the fs commands. 

It was also easier to break it out into its own function as it meant we could handle any errors easier within this solution. As I put it into its own function I decided that creating an array to loop through each solution was easiest to do. Within JavaScript, we needed to show out the split for each time and specify the file type in this case UTF8.

const fs = require('fs')

fs.readFile('..\\input.txt', 'UTF8', (err, data) => {
    if (err) {

    var array = data.split("\n")
    var toDo = ['Part1', 'Part1B', 'Part2', 'Part2B']
    for(let td of toDo) {
        results = callToAction(array, td)
        var val = results[0]
        var val2 = results[1]
        console.log(`${val2} - ${ - val} milli`)


Python again made it a little more simple by allowing up to open a file, again like PowerShell we didn’t need to import anything extra. We then looped through each file ensured it was set to an int and added it into our lines array. We can then use lines to loop through later.

with open('..\\input.txt') as file:
    lines = [int(v) for v in file]

The other biggest difference I found was checking an array to see if it contained a value. 

PowerShell & C#

These two solutions are practically identical and the easiest to understand as it was written as you’d expect.

PowerShell: if ($content.Contains($tofind))
C#: if (content.Contains(toFind))


Similar to PowerShell and C# this also makes a lot of sense written-wise, we’re just swapping if the array ‘contains’ to if the array ‘includes’.

if (array.includes(`${parseInt(toFind)}`))


This was where I found the biggest difference, I found that a lot of Python may be easier to pick up but the solution to loop through code or check to see if something exists is syntactically too similar you have to ensure you’re reading the start of the line to get the jist of what’s happening. Below is the line to check if the array includes our required number.

if toFind in lines:

This is so similar to if you’re just looping an array.

for a in lines:

Personally, it took a little while to really get to grips with how the Python language is written as it doesn’t stand out like the other examples.

After much research and understanding for each language, I was able to get each solution working and I am happy with how it turned out for my first day delving into some of these languages. 

Part 2 of the first day is so similar but requires looping through the file again as now instead of two numbers you’re after 3. If you’d like to look into my solution don’t forget to check out my GitHub page.

If you have any questions please let me know, I will be writing a follow-up blog detailing the time improvements I achieved on this challenge.

Leave a Reply

Your email address will not be published.