AoC 2020 Day 4: A factory of Enums

Carlos A. Ledezma
nPlan
Published in
3 min readJan 15, 2021

This is part of a series we did on the Advent of Code 2020 problems. You can see a list of all the problems we wrote about here.
If you are a fan of AoC, and had fun working on these problems, it is likely that you would be a good fit with our team. Our engineering team had a blast with it, as we like to be challenged with hard problems. If you like what you see, we are constantly
looking for great talent, please consider joining us!

This story is part of our series on AoC 2020

Oh no! It seems like we have brought the incorrect form of ID to the airport. This would normally be a deal-breaker at airport security. However, using our (Python3) coding super powers we can trick the automated systems into letting us in with our North Pole Credentials.

The core of the problem is to verify which passports in the list provided are valid. There are two things to verify: (1) that all the required fields are there and (2) that the fields contain valid values in them.

The rules that define the validity of the fields are given by the problem and can be coded into individual functions. If you want to have some extra fun, use RegEx to match the string patterns (left as an exercise to the reader).

Now that we have these functions, we need a way to assign each field to each function so that when we read a pair (field_name, field_value), the value is passed through the correct verification function.

Here is where Enums come in handy. An Enum is a set of symbolic names that are bound to unique and constant values. In Python 3, you can bind a string (the field identifier) to a function (the verification for the value) like this:

So, we can now run the value of a field through the correct verification function without having to do a flurry of if/else statements. At this moment it can be noted that a similar behaviour can be obtained from a dictionary. However, a dictionary is mutable in Python, whereas an Enum is not. This means that the VerificationFunctions are truly constant. They don’t rely on good coding practices (the constant value shouldn’t be changed), they enforce good practices (the constant value can’t be changed).

This tenet of enforcing certain behaviour can be a life-saver in large codebases. Even more so in large Python codebases where a “constant” can be passed into a function and then mutated inside the function without the knowledge of the person calling the function.

Finally, all that’s left is to write the loop that will parse our passports and count how many of them are valid. No big surprises here:

Even though it is a simple one, this problem helps illustrate the importance of code being consistent with its intention. If something is meant to be a constant map, it pays to make it truly constant. To me, the learning opportunity makes this an interesting problem.

If you would like to see nPlan’s take on other problems from the AoC, do take a look at the other entries in this series. If you are passionate about software engineering and solving difficult problems, visit our careers page! We are constantly looking for talented men and women to join our team.

--

--