Solution to problem 4 part 2 in Python (failed mypy :()

This commit is contained in:
David Doblas Jiménez 2021-11-20 16:42:54 +01:00
parent d9f3206e1d
commit bd85f284fd

139
src/P4.py
View File

@ -160,88 +160,69 @@ def part_1() -> None:
# passports are valid?
# def is_valid(field: str) -> bool:
# # print(f"checking {field}")
# if field[:3] == "byr" and len(field[4:]) == 4:
# # print("byr?")
# if int(field[4:]) > 1920 or int(field[4:]) < 2002:
# # print("passed")
# return True
# if field[:3] == "iyr" and len(field[4:]) == 4:
# # print("iyr?")
# if int(field[4:]) > 2010 or int(field[4:]) < 2020:
# # print("passed")
# return True
# if field[:3] == "eyr" and len(field[4:]) == 4:
# # print("eyr?")
# if int(field[4:]) > 2020 or int(field[4:]) < 2030:
# # print("passed")
# return True
# if (
# field[:3] == "hgt"
# and len(field[4:-2]) > 0
# and type(int(field[4:-2])) == int
# ):
# # print("hgt?")
# if field[-2:] == "cm":
# # print("cm?")
# if int(field[4:-2]) > 150 or int(field[4:-2]) < 193:
# # print("passed")
# return True
# if field[-2:] == "in":
# # print("in?")
# if int(field[4:-2]) > 59 or int(field[4:-2]) < 76:
# # print("passed")
# return True
# if field[:3] == "hcl" and len(field[4:]) == 7:
# # print("hcl?")
# valid_char = 0
# for c in field[5:]:
# if c in "0123456789abcdef":
# valid_char += 1
# if valid_char == 6:
# # print("passed")
# return True
# if field[:3] == "ecl":
# # print("ecl?")
# if field[4:] in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
# # print("passed")
# return True
# if field[:3] == "pid":
# # print("pid?")
# if len(field[4:]) == 9 and field[4:].isdigit():
# # print("passed")
# return True
def is_valid(passport: dict[str, str]) -> bool:
keys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
for key in keys:
if key not in passport:
return False
if not any([passport[key] != 4 for key in ["byr", "iyr", "eyr"]]):
return False
if passport["byr"] < "1920" or passport["byr"] > "2002":
return False
if passport["iyr"] < "2010" or passport["iyr"] > "2020":
return False
if passport["eyr"] < "2020" or passport["eyr"] > "2030":
return False
if not any(["cm" in passport["hgt"], "in" in passport["hgt"]]):
return False
_hgt = int(passport["hgt"][:-2])
if "cm" in passport["hgt"]:
if _hgt < 150 or _hgt > 193:
return False
if "in" in passport["hgt"]:
if _hgt < 59 or _hgt > 76:
return False
if "#" not in passport["hcl"]:
return False
_hcl = passport["hcl"].split("#")[-1]
if len(_hcl) != 6:
return False
try:
int(_hcl, 16)
except ValueError:
return False
if not any(
[
passport["ecl"]
in [
"amb",
"blu",
"brn",
"gry",
"grn",
"hzl",
"oth",
]
]
):
return False
if len(passport["pid"]) != 9:
return False
return True
# def part_2() -> None:
# valid_passports = 0
# for passport in passports:
# if len(passport) > 7:
# # print(f"checking passport: {passport}")
# valid_fields = 0
# for field in passport:
# if is_valid(field):
# valid_fields += 1
# if valid_fields > 7:
# # print("passed")
# valid_passports += 1
# if len(passport) == 7:
# # print(f"checking passport: {passport}")
# valid_fields = 0
# for field in passport:
# if is_valid(field):
# valid_fields += 1
# if "cid" in field:
# pass
# if valid_fields == 7:
# # print("valid passport")
# valid_passports += 1
# # break
# print(f"There are {valid_passports} valid passports")
def part_2() -> None:
valid_passports = 0
for passport in passports:
_items: list[list[str]] = [
item.split(":") for item in passport if item
]
passport_dict: dict[str, str] = dict(_items)
if is_valid(passport_dict):
valid_passports += 1
print(f"There are {valid_passports} valid passports")
if __name__ == "__main__":
part_1()
# part_2()
part_2()