Solve 2020/04

master
Alfred Melch 4 years ago
parent f16dccdee6
commit 573ac5b393

File diff suppressed because it is too large Load Diff

@ -0,0 +1,41 @@
"""
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))))

@ -0,0 +1,72 @@
"""
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 validate_int(value, min, max):
try:
value = int(value)
if value < min or value > max:
return False
return True
except:
return False
def is_valid(passport):
for field in required_fields:
if field not in passport:
return False
if not validate_int(passport['byr'], 1920, 2002):
return False
if not validate_int(passport['iyr'], 2010, 2020):
return False
if not validate_int(passport['eyr'], 2020, 2030):
return False
hgt_val = passport['hgt'][:-2]
hgt_type = passport['hgt'][-2:]
if hgt_type == 'cm':
if not validate_int(hgt_val, 150, 193):
return False
elif hgt_type == 'in':
if not validate_int(hgt_val, 59, 76):
return False
else:
return False
if not re.match('#[0-9a-f]{6}', passport['hcl']):
return False
if not passport['ecl'] in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
return False
print(passport['pid'])
if not len(passport['pid']) == 9 or not validate_int(passport['pid'], 0, 999999999):
return False
return True
print('Count:')
print(len(list(x for x in passports if is_valid(x))))
Loading…
Cancel
Save