""" Solution to 2020/04 """ import re passports = [] with open('input', 'r') as f: for passportRaw in f.read().strip().split('\n\n'): passport = {} for [key, value] in re.findall(r'([\w]{3}):(\S*)', passportRaw): passport[key] = value # print(passportRaw) # print(passport) # print() passports.append(passport) print('Passports:', len(passports)) required_fields = [ 'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid' ] def is_valid(passport): print() print(passport) for field in required_fields: print('field in passport:', field, field in passport) if field not in passport: return False return True print('Count:') print(len(list(x for x in passports if is_valid(x))))