1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- passports = []
- with open("4.input") as f:
- passport = {}
- for line in f.readlines():
- if line.strip() == "":
- passports.append(passport)
- passport = {}
- else:
- pairs = line.strip().split(" ")
- for pair in pairs:
- [key, value] = pair.strip().split(":")
- passport[key] = value
- passports.append(passport)
- def passport_valid(passport):
- required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]
- for key in required_fields:
- if key not in passport.keys():
- return False
- return True
- def passport_valid_extended(passport):
- if not passport_valid(passport):
- return False
- byr = int(passport["byr"])
- if byr not in range(1920, 2003):
- return False
- iyr = int(passport["iyr"])
- if iyr not in range(2010, 2021):
- return False
- eyr = int(passport["eyr"])
- if eyr not in range(2020, 2031):
- return False
- hgt = passport["hgt"]
- if "cm" in hgt:
- if int(hgt[0:-2]) not in range(150, 194):
- return False
- elif "in" in hgt:
- if int(hgt[0:-2]) not in range(59, 77):
- return False
- else:
- return False
- hcl = passport["hcl"]
- if hcl[0] != '#':
- return False
- if len(hcl) != 7:
- return False
- for c in hcl[1:6]:
- if c not in "0123456789abcdef":
- return False
- if passport["ecl"] not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
- return False
- pid = passport["pid"]
- if len(pid) != 9:
- return False
- for c in pid:
- if c not in "0123456789":
- return False
- return True
- valid = filter(passport_valid, passports)
- print(sum(1 for _ in valid))
- valid = filter(passport_valid_extended, passports)
- print(sum(1 for _ in valid))
|