The moment we have all been waiting for, December first. The end of the year is upon us and so those Advent of Code!
To those of you who don’t know, AoC is a yearly programming competition where we try to help Santa do some funky sh**. The competition itself is speed and time based, you can read more about it here.
We are given a list of numbers where each consecutive block of numbers represents the calories an elf is carrying with him. For example
This problem seems simple enough, we need to split the string on empty lines then take each chunk and sum the numbers in it. Once we obtained the various chunks (elf calories) we can search for the maximum value and we have our answer.
Reading our input from a file named “input.txt”
Why are we splitting the string on “\n\n”? The only char in an empty line is “\n”, combining this with the line before we get “\n\n”
Summing the Chunks
Adding a new function that gets a string representing a chunk e.g
1000\n2000\n3000 and returns the sum of those numbers
to convert a string to an int we need to import the
strconv package and use the
atoi function (ascii to int).
Now its probably a good point to mention that in Go you deal with errors by returning them, in the function above the
_ returned from
atoi represent a possible error that we should deal with but since AoC is all about speed we can just tell the compiler to ignore it by naming it
Putting it All Together
Again for the sake of speed we will just store all the results, sort them in ascending order, using Go
sort package and use the last position as our answer.
I know, I know I could have found the max entry by comparing them inside the for loop but you’ll see it will be worth it in part 2.
In part 2 we are asked to return the sum of the 3 elves carrying the most calories i.e take our sorted array and sum the last 3 elements We can easily tweak our answer to accommodate these new requirements by adding this line
I used sorting to get the biggest N elements in the array but there are other more performant approaches here like using a max-heap data structure BUT AoC is about keeping it simple and getting a valid answer ASAP so unless you know you’ll have a performance problem it’s better to go with the simpler and more naive solution most often.
You can find a complete code example here Thanks for reading!
This post is number 2 of a 13 posts series > Learning Go.