Fixed regex, added types

This commit is contained in:
kamoshi 2020-12-04 10:19:40 +01:00 committed by GitHub
parent 0bf582300a
commit 9e1508596e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ def parse_input() -> list[dict]:
with open("input.txt") as file:
for line in file:
line_ = line.rstrip()
if len(line_) > 2:
if len(line_) > 1:
passports[-1].append(line_)
else:
if not passports[-1] == []:
@ -33,7 +33,7 @@ PASSPORTS = parse_input()
def solve_p1() -> int:
def test_passport(passport):
def test_passport(passport: dict) -> bool:
for tag_name in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]:
if tag_name not in passport:
return False
@ -49,23 +49,23 @@ def solve_p1() -> int:
def solve_p2() -> int:
TAGS_REGEX = [
("byr", re.compile(r"(19[2-9]\d)|(200[12])")),
("iyr", re.compile(r"201\d|2020")),
("eyr", re.compile(r"202\d|2030")),
("hgt", re.compile(r"((1[5-8]\d)|(19[0-3]))cm|((59|[67]\d)|7[0-6])in")),
("hcl", re.compile(r"#[0-9a-f]{6}")),
("ecl", re.compile(r"amb|blu|brn|gry|grn|hzl|oth")),
("pid", re.compile(r"\d{9}"))
("byr", re.compile(r"((19[2-9]\d)|(200[012]))$")),
("iyr", re.compile(r"(201\d|2020)$")),
("eyr", re.compile(r"(202\d|2030)$")),
("hgt", re.compile(r"(((1[5-8]\d)|(19[0-3]))cm|((59|[67]\d)|7[0-6])in)$")),
("hcl", re.compile(r"#[0-9a-f]{6}$")),
("ecl", re.compile(r"(amb|blu|brn|gry|grn|hzl|oth)$")),
("pid", re.compile(r"\d{9}$"))
]
def test_tag(passport, tag, pattern: re.Pattern):
def test_tag(passport: dict, tag: str, pattern: re.Pattern) -> bool:
if tag not in passport:
return False
if pattern.match(passport[tag]):
return True
return False
def test_passport_adv(passport):
def test_passport_adv(passport: dict) -> bool:
result = []
for (tag_name, pattern) in TAGS_REGEX:
result.append(test_tag(passport, tag_name, pattern))