Skip to content

Instantly share code, notes, and snippets.

@rtt
Last active December 4, 2020 15:02
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 rtt/d32a6222250de436e003390a57bb7951 to your computer and use it in GitHub Desktop.
Save rtt/d32a6222250de436e003390a57bb7951 to your computer and use it in GitHub Desktop.
import string
def get_input():
with open('input4full.txt') as f:
passwords = f.read()
return passwords.strip()
def run():
passports = [p.replace('\n', ' ').split(' ') for p in get_input().split('\n\n')]
tests = {
'byr': lambda x: int(x) in range(1920, 2003),
'iyr': lambda x: int(x) in range(2010, 2021),
'eyr': lambda x: int(x) in range(2020, 2031),
'hgt': lambda x: (x[-2:] in ['in', 'cm']) and {'in': lambda x: int(x) in range(59,77), 'cm': lambda x: int(x) in range(150,194)}[x[-2:]](x[:-2]),
'hcl': lambda x: x[0] == '#' and all(c in string.hexdigits for c in x[1:]),
'ecl': lambda x: x in {'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'},
'pid': lambda x: all(y in string.digits for y in x) and len(x) == 9,
}
print(sum(all(tests[p[:3]](p[4:]) for p in passport if not p[:3] == 'cid') for passport in passports))
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment