You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day4a.py 883B

1234567891011121314151617181920212223242526272829303132333435
  1. import re
  2. def valid(ppt):
  3. k = ppt.keys()
  4. for v in ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]:
  5. if v not in k:
  6. print(f"Missing {v}.")
  7. return False
  8. return True
  9. def main():
  10. with open("input4.txt") as file:
  11. text = file.read()
  12. ppts = text.split("\n\n")
  13. print(f"I have {len(ppts)} passports.")
  14. count = 0
  15. cinv = 0
  16. for ppt in ppts:
  17. ppt = ppt.replace("\n", " ")
  18. pptchunks = ppt.split(" ")
  19. pptdct = {}
  20. for chunk in pptchunks:
  21. if chunk != "":
  22. bits = chunk.split(":")
  23. pptdct[bits[0]] = bits[1]
  24. # print(ppt)
  25. # print(pptdct)
  26. if valid(pptdct):
  27. count += 1
  28. else:
  29. cinv += 1
  30. print(f"There are {count} valid passports and {cinv} invalid ones.")
  31. if __name__ == "__main__":
  32. main()