Skip to content
advent of code 2020

day 04

passport processing

approach

a generator turns the blank-line-separated records into dictionaries of key: value fields. part 1 counts the records that contain all seven required keys.

part 2 also checks each value. there is one small validator per field (year ranges, a height with a cm or in unit, a hex colour and a nine-digit id via regular expressions, an eye colour from a fixed list), and a dictionary maps each field name to its validator so the check stays one line.

the trick

map each field to its own validator, and a record is valid when every field is present and passes.

python
    fields = {
            "byr": val_byr,
            "iyr": val_iyr,
            "eyr": val_eyr,
            "hgt": val_hgt,
            "hcl": val_hcl,
            "ecl": val_ecl,
            "pid": val_pid
            }
    for doc in d:
        res += all([f in doc and fields[f](doc[f]) for f in fields])
    return res