You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
813 B
Python
42 lines
813 B
Python
4 years ago
|
"""
|
||
|
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))))
|