AoC 2020 D4: Passport Processing
| Problem statement | Source code | Tags: String manipulation
← Previous Back to AoC Index Next →
Part 1
For each passport, we first collect the fields as (key, value) pairs in a dictionary:
Then we check if all required fields are present:
(The required dictionary is defined for part 2.)
Part 2
When I first did part 1, required was just a set of field names. For part 2, I changed it to a dictionary mapping field names to validation functions. Different fields have different validation rules, so this makes it easy to organize them:
The validation rules include range checks, string pattern checks, and a mix of both (for hgt).
The problem is vague about what to do with duplicate fields, so for duplicate fields, I immediately invalidate the passport, and only validate passports with unique fields. This is straightforward in Python with for...else: