Skip to content
Home » Daily Habits: The Advent of Code

Daily Habits: The Advent of Code

Today is the day that the lazy coder in me has been waiting for all year.

I like the challenge of the Advent of Code – or rather I like it until it becomes too hard (and therefore boring) for me. It serves as an annual reminder of a few things:

  1. How much I’ve forgotten since last year.
  2. Coding is never about the code. It’s about the puzzle we’re trying to unlock.
  3. You can be bitten by bad code in an astoundingly short amount of time.

Now, I’m not one of those who will spend much time optimising my code. As long as it runs and it works then I’m happy. I’m also not the type to go and write many tests alongside my advent of code daily entries. I do, however, like to put them into a folder structure, and I do like to create just enough structure in my code to make it legible and reusable when I return to it. I’ll also put in comments to explain what I’m doing.

I’ve never been one of those “the code is self-explanatory” coders. For me, this argument doesn’t hold any water. You can always ease someone’s way by writing some comments in a complex algorithm. Then, returning to it later, you might be able to optimise or understand your thought processes more easily.

For example, here’s a piece of code I just wrote for the first challenge this year:

    for char in line:            
        if char.isnumeric():
            nums.append(char)
            latestNumberWord: str = "" # reset
        else:
            if (mapNumberWords):
                latestNumberWord += char

                # now we need to reverse build the latest matching numberWord
                i = 0
                findNumber: str = ""
                while i < len(latestNumberWord):
                    findNumber = latestNumberWord[len(latestNumberWord) - i  - 1] + findNumber

                    if findNumber in mapNumbers.keys():
                        i = len(latestNumberWord) + 1
                    else:
                        i += 1
                
                if (i == len(latestNumberWord) + 1):
                    nums.append(mapNumbers[findNumber])

We can see that we’re moving through the characters in a line, if the character is a numeric then we simply add it to an outer array of numbers and continue. If the char is non-numeric (in this case it will always be an alphabetic character due to the input data) then we will add it to the latestNumberWord. Then we need to match the latest alphabetic spelling of a number to that growing string – this is where the reverse-build comes in to match for example ‘two’ in the input string and map it to an actual number before appending it again to the mapNumbers array.

This is not the prettiest or neatest algorithm and many short cuts could be taken by pythonistas to do this perhaps in a single line or two. But that’s never been my style. Why? Because I want the next person of average ability – just like myself – to be able to add to this code in the future.

Advent of Code is what you make it. For me, it’s about remembering that you don’t need to be super-clever to get the right answer.

Daily Habits

I find myself increasingly talking about daily habits and have done since reading and getting into James Clear’s Atomic Habits. I’ve given a couple of talks this year mentioning the book but more explicitly about the habits. For me, things don’t need to be necessarily daily, but at least if I keep some habits regular then I find it easier to map improvements or degradations.

This for me is the essence of going through moments in life like the Advent of Code. By forcing yourself to study, to learn, to remember, to stretch yourself, you’ll always be looking beyond the things you have now. As a coder or as a manager, if you work in software engineering, give yourself a few moments to try the Advent of Code and remember what it feels like to have that unencumbered starting point which is so exciting at the beginning of all projects.

Also see…

If you like the Advent of Code, you might also enjoy the codewars katas. I spent some time this year doing these before an interview, which never included any Python tests…

If you’d like to follow along with my attempts (until I get bored, you have be warned at least twice now) you can check out my github.





Discover more from Richard Bown

Subscribe now to keep reading and get access to the full archive.

Continue reading