3 REQUIRED_FIELDS=(('byr', "Birth Year"),('iyr', "Issue Year"),('eyr', "Expiration Year"),('hgt', "Height"),('hcl', "Hair Colour"),('ecl', "Eye Colour"),('pid',"Passport ID"))
4 OPTIONAL_FIELDS=(('cid', "Country ID"))
5 VALID_EYE_COLOURS=('amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth')
7 def check_passport(passport_data):
9 for field in REQUIRED_FIELDS:
10 if field[0] not in passport_data:
12 # check the field data
14 if not check_year(passport_data[field[0]], 1920, 2002):
16 elif field[0] == 'iyr':
17 if not check_year(passport_data[field[0]], 2010, 2020):
19 elif field[0] == 'eyr':
20 if not check_year(passport_data[field[0]], 2020, 2030):
22 elif field[0] == 'hgt':
23 if not check_height(passport_data[field[0]]):
25 elif field[0] == 'hcl':
26 if not check_hair_colour(passport_data[field[0]]):
28 elif field[0] == 'ecl':
29 if not passport_data[field[0]] in VALID_EYE_COLOURS:
31 elif field[0] == 'pid':
32 if not check_pid(passport_data[field[0]]):
36 def check_year(data, min_year, max_year):
39 if year >= min_year and year <= max_year:
45 def check_height(data):
48 value = int(data[:-2])
51 if value >= 150 and value <= 193:
54 if value >= 59 and value <= 76:
60 def check_hair_colour(data):
65 if data != data.lower():
68 value = int(data[1:], 16)
75 # first check that the string length is correct
79 # now check that it's a number
90 for line in open("input.txt"):
92 if check_passport(passport_data):
100 parts=line.split(" ")
102 (key, value) = part.split(":")
103 passport_data[key] = value
105 if check_passport(passport_data):
107 print("Found", valid_passports, "valid passports")