Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # checks = {"byr": True, "iyr": True, "eyr": True, "hgt": True, "hcl": True, "ecl": True, "pid": True}
- # passed = True
- # for value in checks.values():
- # passed = passed * value
- # print(passed)
- # Read all the lines, group them by blank line between them
- passports = []
- with open("4 - input", 'r') as file:
- lines = file.readlines()
- entry = []
- for line in lines:
- if line != "\n":
- for item in line.strip("\n").split(" "):
- entry.append(item)
- if line == "\n" or line is lines[-1]:
- passports.append(entry)
- entry = []
- # print(passports)
- # list now contains lines as string elements key:value, split into dicts instead {key : value, key: value, etc},
- # append all dicts to a new list
- passportDicts = []
- for passport in passports:
- entryDict = {}
- for entry in passport:
- entryDict[entry.split(":")[0]] = entry.split(":")[1]
- passportDicts.append(entryDict)
- # Sort the list by length of the dicts for some nice printing
- # passportDicts = sorted(passportDicts, key=lambda x: -len(x))
- # print(passportDicts)
- validPassports = 0
- for passport in passportDicts:
- sortedKeys = list(passport.keys()) # Get and sort the list of keys in each dict
- sortedKeys.sort()
- validKeys = False
- if len(passport.keys()) == 8: # Dict contains all eight keys, passport is valid
- validKeys = True
- elif len(passport.keys()) == 7:
- containsCID = False
- if "cid" in list(passport.keys()): # Dict is missing key other than cid
- validKeys = False
- else: # Dict contain every other key but cid
- validKeys = True
- if validKeys:
- print("Entry contains all valid keys", end="")
- checks = {"byr": False, "iyr": False, "eyr": False, "hgt": False, "hcl": False, "ecl": False, "pid": False}
- if 1920 <= int(passport["byr"]) <= 2002:
- checks["byr"] = True
- print(f", byr is between 1920 and 2020 [{int(passport['byr'])}]", end="")
- else:
- print(", INVALID BYR")
- continue
- if 2010 <= int(passport["iyr"]) <= 2020:
- checks["iyr"] = True
- print(f", iyr is between 1920 and 2020 [{int(passport['iyr'])}]", end="")
- else:
- print(", INVALID IYR")
- continue
- if 2020 <= int(passport["eyr"]) <= 2030:
- checks["eyr"] = True
- print(f", eyr is between 1920 and 2020 [{int(passport['eyr'])}]", end="")
- else:
- print(", INVALID EYR")
- continue
- if passport["hgt"][-2:] == "cm":
- if 150 <= int(passport["hgt"][:-2]) <= 193:
- checks["hgt"] = True
- print(f", hgt in cm is between 150 and 193 [{int(passport['hgt'][:-2])}]", end="")
- elif passport["hgt"][-2:] == "in":
- if 59 <= int(passport["hgt"][:-2]) <= 76:
- checks["hgt"] = True
- print(f", hgt in in is between 59 and 76 [{int(passport['hgt'][:-2])}]", end="")
- else:
- print(", INVALID HGT")
- continue
- if passport["hcl"][0] == "#" and len(passport["hcl"]) == 7:
- numRange = list(range(0, 10))
- charRange = ['a', 'b', 'c', 'd', 'e', 'f', '#']
- validRange = numRange + charRange
- validHex = True
- for char in passport["hcl"]:
- convChar = 0
- if char.isdigit():
- convChar = int(char)
- else:
- convChar = char
- if convChar not in validRange:
- validHex = False
- if validHex:
- checks["hcl"] = True
- print(f", hcl is valid hex [{passport['hcl']}]", end="")
- else:
- print(f", INVALID HCL, invalid hex: [{passport['hcl']}]")
- continue
- else:
- print(", INVALID HCL, not right len")
- continue
- if len(passport["ecl"]) == 3:
- validEyes = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
- if passport["ecl"] in validEyes:
- checks["ecl"] = True
- print(f", ecl is valid color [{passport['ecl']}]", end="")
- else:
- print(", INVALID ECL")
- continue
- else:
- print(", INVALID ECL")
- continue
- if len(passport["pid"]) == 9:
- numRange = list(range(0, 10))
- validPID = True
- for char in passport["pid"]:
- if int(char) not in numRange:
- validPID = False
- if validPID:
- print(f", pid is valid [{passport['pid']}]", end="")
- checks["pid"] = validPID
- else:
- print(", INVALID PID")
- continue
- else:
- print(", INVALID PID")
- continue
- passed = True
- for value in checks.values():
- passed = passed * value
- if passed:
- print(" ...VALID")
- validPassports += 1
- else:
- print(" ...INVALID!")
- print(validPassports)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement