aoc2020/04/2.py

16 lines
867 B
Python
Raw Normal View History

2020-12-04 09:35:58 +00:00
sections = open("input", "r").read().split("\n\n")
sections = [section.replace("\n", " ") for section in sections]
passports = [dict([s.split(":") for s in section.split(" ") if s]) for section in sections]
import re
validators = {
"byr": ("[0-9]{4}$", lambda a: 1920 <= int(a) <= 2002),
"iyr": ("[0-9]{4}$", lambda a: 2010 <= int(a) <= 2020),
"eyr": ("[0-9]{4}$", lambda a: 2020 <= int(a) <= 2030),
"hgt": ("[0-9]+(in|cm)$", lambda a: 150 <= int(a.replace("cm", "")) <= 193 if "cm" in a else 59 <= int(a.replace("in", "")) <= 76),
"hcl": ("#[0-9a-f]{6}$", lambda a: True),
"ecl": ("(amb|blu|brn|gry|grn|hzl|oth)$", lambda a: True),
"pid": ("[0-9]{9}$", lambda a: True),
}
valid = [p for p in passports if all([k in p and re.match(r, p[k]) and v(p[k]) for (k, (r, v)) in validators.items()])]
print(len(valid))