Skip to content

Instantly share code, notes, and snippets.

@redspider
Last active December 4, 2020 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redspider/5d3374654d0b228259e63e54b1b37879 to your computer and use it in GitHub Desktop.
Save redspider/5d3374654d0b228259e63e54b1b37879 to your computer and use it in GitHub Desktop.
import re
REQUIRED_KEYS = [
'byr',
'iyr',
'eyr',
'hgt',
'hcl',
'ecl',
'pid'
]
passports = []
current_passport = ""
for line in INPUT.split("\n"):
if line == "":
passports.append(dict([kv.split(":") for kv in current_passport.strip().split(" ")]))
current_passport = ""
else:
current_passport += " " + line
# Part 1
print(len([p for p in passports if set(REQUIRED_KEYS).issubset(set(p.keys()))]))
# Part 2
def is_valid(p):
if not set(REQUIRED_KEYS).issubset(set(p.keys())):
return False
if not re.match(r'^\d\d\d\d$', p['byr']):
return False
if not 1920 <= int(p['byr']) <= 2002:
return False
if not re.match(r'^\d\d\d\d$', p['iyr']):
return False
if not 2010 <= int(p['iyr']) <= 2020:
return False
if not re.match(r'^\d\d\d\d$', p['eyr']):
return False
if not 2020 <= int(p['eyr']) <= 2030:
return False
m = re.match(r'(\d+)(cm|in)', p['hgt'])
if not m:
return False
height, type = m.groups()
if type == 'cm':
if not 150 <= int(height) <= 193:
return False
elif type == 'in':
if not 59 <= int(height) <= 76:
return False
if not re.match(r'^#[0-9a-f]{6}$', p['hcl']):
return False
if p['ecl'] not in ['amb','blu','brn','gry','grn','hzl','oth']:
return False
if not re.match(r'^[0-9]{9}$', p['pid']):
return False
return True
print(len([p for p in passports if is_valid(p)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment