Armandur

Untitled

Dec 4th, 2020
455
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # checks = {"byr": True, "iyr": True, "eyr": True, "hgt": True, "hcl": True, "ecl": True, "pid": True}
  2. # passed = True
  3. # for value in checks.values():
  4. #     passed = passed * value
  5. # print(passed)
  6.  
  7. #  Read all the lines, group them by blank line between them
  8.  
  9. passports = []
  10. with open("4 - input", 'r') as file:
  11.     lines = file.readlines()
  12.     entry = []
  13.     for line in lines:
  14.         if line != "\n":
  15.             for item in line.strip("\n").split(" "):
  16.                 entry.append(item)
  17.         if line == "\n" or line is lines[-1]:
  18.             passports.append(entry)
  19.             entry = []
  20. # print(passports)
  21.  
  22. #   list now contains lines as string elements key:value, split into dicts instead {key : value, key: value, etc},
  23. #   append all dicts to a new list
  24.  
  25. passportDicts = []
  26. for passport in passports:
  27.     entryDict = {}
  28.     for entry in passport:
  29.         entryDict[entry.split(":")[0]] = entry.split(":")[1]
  30.     passportDicts.append(entryDict)
  31.  
  32. #   Sort the list by length of the dicts for some nice printing
  33.  
  34. # passportDicts = sorted(passportDicts, key=lambda x: -len(x))
  35. # print(passportDicts)
  36.  
  37. validPassports = 0
  38. for passport in passportDicts:
  39.     sortedKeys = list(passport.keys())  # Get and sort the list of keys in each dict
  40.     sortedKeys.sort()
  41.  
  42.     validKeys = False
  43.     if len(passport.keys()) == 8:  # Dict contains all eight keys, passport is valid
  44.         validKeys = True
  45.     elif len(passport.keys()) == 7:
  46.         containsCID = False
  47.         if "cid" in list(passport.keys()):  # Dict is missing key other than cid
  48.             validKeys = False
  49.         else:  # Dict contain every other key but cid
  50.             validKeys = True
  51.  
  52.     if validKeys:
  53.         print("Entry contains all valid keys", end="")
  54.         checks = {"byr": False, "iyr": False, "eyr": False, "hgt": False, "hcl": False, "ecl": False, "pid": False}
  55.         if 1920 <= int(passport["byr"]) <= 2002:
  56.             checks["byr"] = True
  57.             print(f", byr is between 1920 and 2020 [{int(passport['byr'])}]", end="")
  58.         else:
  59.             print(", INVALID BYR")
  60.             continue
  61.  
  62.         if 2010 <= int(passport["iyr"]) <= 2020:
  63.             checks["iyr"] = True
  64.             print(f", iyr is between 1920 and 2020 [{int(passport['iyr'])}]", end="")
  65.         else:
  66.             print(", INVALID IYR")
  67.             continue
  68.  
  69.         if 2020 <= int(passport["eyr"]) <= 2030:
  70.             checks["eyr"] = True
  71.             print(f", eyr is between 1920 and 2020 [{int(passport['eyr'])}]", end="")
  72.         else:
  73.             print(", INVALID EYR")
  74.             continue
  75.  
  76.         if passport["hgt"][-2:] == "cm":
  77.             if 150 <= int(passport["hgt"][:-2]) <= 193:
  78.                 checks["hgt"] = True
  79.                 print(f", hgt in cm is between 150 and 193 [{int(passport['hgt'][:-2])}]", end="")
  80.         elif passport["hgt"][-2:] == "in":
  81.             if 59 <= int(passport["hgt"][:-2]) <= 76:
  82.                 checks["hgt"] = True
  83.                 print(f", hgt in in is between 59 and 76 [{int(passport['hgt'][:-2])}]", end="")
  84.         else:
  85.             print(", INVALID HGT")
  86.             continue
  87.  
  88.         if passport["hcl"][0] == "#" and len(passport["hcl"]) == 7:
  89.             numRange = list(range(0, 10))
  90.             charRange = ['a', 'b', 'c', 'd', 'e', 'f', '#']
  91.             validRange = numRange + charRange
  92.  
  93.             validHex = True
  94.             for char in passport["hcl"]:
  95.                 convChar = 0
  96.                 if char.isdigit():
  97.                     convChar = int(char)
  98.                 else:
  99.                     convChar = char
  100.                 if convChar not in validRange:
  101.                     validHex = False
  102.  
  103.             if validHex:
  104.                 checks["hcl"] = True
  105.                 print(f", hcl is valid hex [{passport['hcl']}]", end="")
  106.             else:
  107.                 print(f", INVALID HCL, invalid hex: [{passport['hcl']}]")
  108.                 continue
  109.         else:
  110.             print(", INVALID HCL, not right len")
  111.             continue
  112.  
  113.         if len(passport["ecl"]) == 3:
  114.             validEyes = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
  115.             if passport["ecl"] in validEyes:
  116.                 checks["ecl"] = True
  117.                 print(f", ecl is valid color [{passport['ecl']}]", end="")
  118.             else:
  119.                 print(", INVALID ECL")
  120.                 continue
  121.         else:
  122.             print(", INVALID ECL")
  123.             continue
  124.  
  125.         if len(passport["pid"]) == 9:
  126.             numRange = list(range(0, 10))
  127.             validPID = True
  128.             for char in passport["pid"]:
  129.                 if int(char) not in numRange:
  130.                     validPID = False
  131.  
  132.             if validPID:
  133.                 print(f", pid is valid [{passport['pid']}]", end="")
  134.                 checks["pid"] = validPID
  135.             else:
  136.                 print(", INVALID PID")
  137.                 continue
  138.         else:
  139.             print(", INVALID PID")
  140.             continue
  141.  
  142.         passed = True
  143.  
  144.         for value in checks.values():
  145.             passed = passed * value
  146.  
  147.         if passed:
  148.             print(" ...VALID")
  149.             validPassports += 1
  150.         else:
  151.             print(" ...INVALID!")
  152.  
  153. print(validPassports)
  154.  
RAW Paste Data