import re from functools import reduce lines = (list(map(lambda a: a.split(':'), x.strip().split())) for x in open("input.txt")) passport = {} count = 0 must_have = {'iyr', 'hcl', 'hgt', 'pid', 'ecl', 'eyr', 'byr'} def check_passport(pp): valid = len(set(pp).intersection(must_have)) == len(must_have) if not valid: return 0 checks = [ re.match("^[0-9]{4}$", pp['byr']) and 1920 <= int(pp['byr']) <= 2002, re.match("^[0-9]{4}$", pp['iyr']) and 2010 <= int(pp['iyr']) <= 2020, re.match("^[0-9]{4}$", pp['eyr']) and 2020 <= int(pp['eyr']) <= 2030, re.match("^(1([5-8][0-9]|9[0-3])cm|(59|6[0-9]|7[0-6])in)$", pp['hgt']), re.match("^#[0-9a-f]{6}$", pp['hcl']), re.match("^(amb|blu|brn|gry|grn|hzl|oth)$", pp['ecl']), re.match("^[0-9]{9}$", pp['pid']) ] if not reduce(lambda a, b: a and b, checks, True): return 0 return 1 for line in lines: if len(line) == 0: count += check_passport(passport) passport = {} for key, val in line: passport[key] = val count += check_passport(passport) print(count)