<jupyter_start><jupyter_text>Part 1<jupyter_code>passports_seg = [[seg.split(':')[0] for seg in passport.split()] for passport in passports]
np.sum([ 1 if len(p)==8 else 1 if len(p)==7 and opt not in p else 0 for p in passports_seg])<jupyter_output><empty_output><jupyter_text>Part 2  Rules- byr (Birth Year) - four digits; at least 1920 and at most 2002.- iyr (Issue Year) - four digits; at least 2010 and at most 2020.- eyr (Expiration Year) - four digits; at least 2020 and at most 2030.- hgt (Height) - a number followed by either cm or in:- If cm, the number must be at least 150 and at most 193.- If in, the number must be at least 59 and at most 76.- hcl (Hair Color) - a  followed by exactly six characters 0-9 or a-f.- ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.- pid (Passport ID) - a nine-digit number, including leading zeroes.- cid (Country ID) - ignored, missing or not.<jupyter_code>passports_seg_dict = [{seg.split(':')[0]: seg.split(':')[1] for seg in passport.split()} for passport in passports]
filtered_passports = [p for p in passports_seg_dict if len(p.keys())==8 or (len(p.keys())==7 and opt not in p.keys())]
leng = lambda x, y: len(x)==y
mini = lambda x, y: int(x)>=y
maxi = lambda x, y: int(x)<=y
cont = lambda x, y: x in y

rules = {
    'byr': {'mini': 1920, 'maxi': 2002},
    'iyr': {'mini': 2010, 'maxi': 2020},
    'eyr': {'mini': 2020, 'maxi': 2030},
    'hgt': {'cm': {'mini': 150, 'maxi': 193},
            'in': {'mini': 59, 'maxi': 76}}
}
import re
if not 0:
    print('car')
True*False
validos = []
for pass_dict in filtered_passports:
    good = True
    for key in pass_dict.keys():
        val = pass_dict[key]
        if key in rules.keys():
            rul = rules[key]
        
        if key in ['byr', 'iyr', 'eyr']:
            b = mini(val, rul['mini'])
            c = maxi(val, rul['maxi'])
            if (b*c) == 0:
                good = False
                break
                
        elif key == 'hgt':
            if 'cm' in val:
                if (not mini(val[:-2], rul['cm']['mini']) or
                    not maxi(val[:-2], rul['cm']['maxi'])):
                    good = False
                    break
            elif 'in' in val:
                if (not mini(val[:-2], rul['in']['mini']) or
                    not maxi(val[:-2], rul['in']['maxi'])):
                    good = False
                    break
            else:
                good = False
                break
                
        elif key == 'hcl':
            if val[0] == '#':
                match = re.findall('^([a-f0-9]{6})$', val[1:])
                if len(match) != 1:
                    good = False
                    break
            else:
                good = False
                break
                    
        elif key == 'ecl':
            if val not in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
                good = False
                break
                
        elif key == 'pid':
            match = re.findall('^([0-9]{9})$', val)
            if len(match) != 1:
                good = False
                break
        
    if good == True:
        validos.append(pass_dict) 
len(filtered_passports)
len(validos)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1511056511056511, 'fraction_numerical': 0.030098280098280097, 'mean_word_length': 1.7625106022052588, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '328800', 'license': 'MIT', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1036, 'n_words': 353, 'path': 'Day4.ipynb', 'repo_name': 'jsrv/advent_of_code', 'size': '3256'}