68 lines
1.5 KiB
Python
68 lines
1.5 KiB
Python
![]() |
#!/usr/bin/env python
|
||
|
|
||
|
import re
|
||
|
|
||
|
passport_req = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'}
|
||
|
|
||
|
def check_passport(passport):
|
||
|
if len(passport_req.difference(set(passport.keys()))) > 0:
|
||
|
return 0
|
||
|
if len(passport.get('byr')) != 4:
|
||
|
return 0
|
||
|
if int(passport.get('byr')) < 1920:
|
||
|
return 0
|
||
|
if int(passport.get('byr')) > 2002:
|
||
|
return 0
|
||
|
|
||
|
if len(passport.get('iyr')) != 4:
|
||
|
return 0
|
||
|
if int(passport.get('iyr')) < 2010:
|
||
|
return 0
|
||
|
if int(passport.get('iyr')) > 2020:
|
||
|
return 0
|
||
|
|
||
|
if len(passport.get('eyr')) != 4:
|
||
|
return 0
|
||
|
if int(passport.get('eyr')) < 2020:
|
||
|
return 0
|
||
|
if int(passport.get('eyr')) > 2030:
|
||
|
return 0
|
||
|
|
||
|
if re.match('^#[0-9a-f]{6}$', passport.get('hcl')) is None:
|
||
|
return 0
|
||
|
|
||
|
if passport.get('ecl') not in ('amb','blu','brn','gry','grn','hzl','oth'):
|
||
|
return 0
|
||
|
|
||
|
if re.match('^[0-9]{9}$', passport.get('pid')) is None:
|
||
|
return 0
|
||
|
|
||
|
if passport.get('hgt')[-2:] == 'cm':
|
||
|
if int(passport.get('hgt')[0:-2]) < 150:
|
||
|
return 0
|
||
|
if int(passport.get('hgt')[0:-2]) > 193:
|
||
|
return 0
|
||
|
elif passport.get('hgt')[-2:] == 'in':
|
||
|
if int(passport.get('hgt')[0:-2]) < 59:
|
||
|
return 0
|
||
|
if int(passport.get('hgt')[0:-2]) > 76:
|
||
|
return 0
|
||
|
else:
|
||
|
return 0
|
||
|
return 1
|
||
|
|
||
|
|
||
|
passport = dict()
|
||
|
valid = 0
|
||
|
with open("input01.txt","r") as f:
|
||
|
for line in f:
|
||
|
line = line.strip()
|
||
|
if len(line) == 0:
|
||
|
valid += check_passport(passport)
|
||
|
passport=dict()
|
||
|
continue
|
||
|
passport.update({x.split(":")[0]:x.split(":")[1] for x in line.split(" ")})
|
||
|
|
||
|
valid += check_passport(passport)
|
||
|
|
||
|
print(valid)
|