import re def valid(ppt): k = ppt.keys() for v in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]: if v not in k: print(f"Missing {v}.") return False return True def main(): with open("input4.txt") as file: text = file.read() ppts = text.split("\n\n") print(f"I have {len(ppts)} passports.") count = 0 cinv = 0 for ppt in ppts: ppt = ppt.replace("\n", " ") pptchunks = ppt.split(" ") pptdct = {} for chunk in pptchunks: if chunk != "": bits = chunk.split(":") pptdct[bits[0]] = bits[1] # print(ppt) # print(pptdct) if valid(pptdct): count += 1 else: cinv += 1 print(f"There are {count} valid passports and {cinv} invalid ones.") if __name__ == "__main__": main()