3 min read

Advent of Code 2020 Day 4

David talks through his day 4 advent of code 2020 solution, and reminds you to test your regex.
Advent of Code 2020 Day 4

I'm still racking my brain around trying to do the file reading for this day's puzzle, but still just don't think it's possible with Java 8 streams (which is kind of unfortunate because I think today's puzzle might be a future pattern in other puzzles).  Nevertheless, I created some Java objects to handle the structure of the data (probably not the best approach, but it ended up working for my solution) and I found that yet again, something small and dumb caused me problems, but more on that later.

Today's puzzle was to parse through a file to pull out individual passports from a file (passports are separated by a blank line in the file) and the passport data may be on multiple lines within the file.  This situation of the individual passports in a file is why I was worried about being able to do this in a streaming fashion (I might be able to hack a way, which I think I want to do for the future). Anyway, once I got the data from the file into a list of these Passport objects that I created it was introducing some logic within the Passport object to validate both the attributes for the Passport as well as eventually the values stored for those attributes of the Passport.

Since the problem statement said to ignore one of the passport attributes, I figure this type of data (the Passport data in a file) will be used in a future puzzle for Advent of Code, then again...I've been wrong before (I'd link out to instances, but I've deleted all history of me being wrong, to make people think I'm always right). So, with this, my validation has been put into Passport-level methods that validate different things, and also methods that can approach those things from a larger level (checking for presence of attributes, and checking for valid values for those attributes). Since I stored the attributes as effectively key/value stores within a list, I was able to use streams to iterate through those items to find a valid instance of an entry for the Passport that conforms to the criteria. The reason why I think this Passport will be used in the future is that one of the items described was "ignored" in this version, that seems to happen when there's further days happening. I coded the logic for validation into a switch statement for each different process.

For now, I didn't really come up with anything super new and exciting for learning, other than making sure your regex is correct and sufficient, for the passport id (9 numeric characters with included leading zeros) I wrote unit tests for each different input type to confirm that it was working properly, and found that my passport id regex, I had [0-9]{9}, was insufficient, as it would cover a case where there were more than 9 numbers in the passport ID, which is why my count was originally higher than it should've been.  By changing my regex to ^[0-9{9}$ I resolved the issue. If you ever need to do some regex testing, I suggest looking at regexr as it provides pretty easy testing and configuration (and explanation) of your regex.

Regex I used for part 2 processing the passport ID, left is the buggy regex, right was the correct regex.

Anyway, as normal, thank you for reading my blog and letting me vent about my programming and logic stupidity. Again, if you want to join my Advent of Code leaderboard, feel free to join with the code: 699615-aae0e8af. I'm thinking I might stay up until the day 5 puzzle is released tonight since I don't have to do work tomorrow.