Advent of Code 2021

Advent of Code 2021

It’s that time of year again, where I start completing the Advent of Code challenges. Each day you’re given 2 puzzles (the second one unlocks if you complete the first one successfully). The puzzles are often to help Santa one way or another to make it through giving presents to everyone around the world.

The first set of puzzles have now been opened for 2021. As a bit of back story to myself, I generally work through these puzzles in PowerShell first.

PowerShell isn’t the fastest language to complete these puzzles in, but I find it easy enough to take the input and manipulate to get the results of the puzzle.

I have compiled my years results and working outs over at my github page. There are incomplete code along with solutions within the github page. I have already started to fill up this years folder with the new code.

I did in 2021 decide to go through some of 2020 and produce some of the code in PowerShell, C#, JavaScript and Python. Although there are differences within the languages I found it wasn’t to difficult to find the language equivalent function to complete the challenge.

The only issue I have is that none of it is optimised and there are probably not only faster but more beautiful ways to finish any of the solutions I post.

So lets have a look at 2021, I’ll give a quick overview and then my solutions below. You can also view them on my github page as well.

This post is not only for myself to document my working but also a way to get feedback. Maybe start a discussion on other methods, better methods for completing the task.

Link to the Advent of Code challenge (Day 1 2021)

Backstory:
An Elf has knocked Santa’s sleigh keys off the side of a boat. They have an experimental antenna which should be able to track the keys.

Part 1:
The job is to take the input that’s provided and find out how many times the depth you go down is greater than the previous depth that was recorded.

Example:

199
200
208
210
200
207
240
269
260
263
199 (N/A - no previous measurement)
200 (increased)
208 (increased)
210 (increased)
200 (decreased)
207 (increased)
240 (increased)
269 (increased)
260 (decreased)
263 (increased)

So any time it has increased we’re looking to record the results and as there is no previous results in the first input then we discard this.

Solution:

$content = get-content(".\input.txt")
$previous, $count = 0

for ($i = 0; $i -lt $content.Length; $i++) {
    [int]$num = $content[$i]
    if ($i -eq 0) {
        write-host "$($num) (N/A - No previous measurement)"
        continue
    } 

    if ($num -gt $previous) { 
        write-host "$($content[$i]) (increase)"
        $count++
    } else {
        write-host "$($content[$i]) (decrease)"
    }
    $previous = $content[$i]
}

write-host "$($count)"

Part 2:
Next question was not looking at the previous input but we’re looking for the combined result of 3 inputs. So it’s be is (199 + 200 + 208) greater than (200 + 208 + 210)?

Solution:

$content = get-content(".\input.txt")
$previous, $count = 0

for ($i = 0; $i -lt $content.Length; $i++) { 
    [int]$CurrentRowNum = $([int]$content[$i] + [int]$content[$i + 1] + [int]$content[$i + 2])

    if ([int]$content[$i + 2] -eq "") { 
        continue
    }

    if ($i -eq 0) {
        write-host "$($CurrentRowNum) (N/A - No previous measurement)"
        continue
    } 

    if ($CurrentRowNum -gt $PreviousRowNum) { 
        write-host "$($CurrentRowNum) (increase)"
        $count++
    } else {
        write-host "$($CurrentRowNum) (decrease)"
    }
    $PreviousRowNum = $CurrentRowNum
}

To note:
There was one issue I was getting when first trying to complete this challenge. When I was doing the test input that was provided I was getting the result I was expecting. When using the full puzzle input I was getting the incorrect answer.

It turns out that I was comparing string values. In order to mitigate this issue I had to infer that that values I was checking against were [int] values. This then provided the correct results.

Let me know what you think below, would you have done it differently? Are there things I could have done to improve my solution. I’ve managed to keep it quite verbose so I can track and easily read and share.

Leave a Reply

Your email address will not be published.