day = "day4" file = f"inputs/{day}.txt" passp = ["byr","iyr","eyr","hgt","hcl","ecl","pid"] # "cid def part_one(file): result = 0 input = open(file, "r") input = input.read() inputs = input.split("\n\n") for item in inputs: if(check(item)): result += 1 print(f"Part 1: {result}") def check(item): bool = True for ps in passp: if(ps in item): continue else: bool = False break return bool part_one(file) def part_two(file): result = 0 input = open(file, "r") input = input.read().split("\n\n") for line in input: if line_check(line): result += 1 print(f"Part 2: {result}") def line_check(line): dict = {} line = line.replace("\n", " ").split(" ") bool = True for code in line: key_value = code.split(":") dict[key_value[0]] = key_value[1] while bool is not False: if (int(dict["byr"]) >= 1920) and (int(dict["byr"]) <= 2020): continue elif (int(dict["iyr"]) >= 2010) and (int(dict["iyr"]) <= 2020): continue elif (int(dict["eyr"]) >= 2020) and (int(dict["eyr"]) <= 2030): continue elif ("cm" in dict["hgt"]) or ("in" in dict["hgt"]): if("cm" in dict["hgt"]): if (int(dict["hgt"]) >= 150) and (int(dict["hgt"]) <= 193): continue elif ("in" in dict["hgt"]): if (int(dict["hgt"]) >= 59) and (int(dict["hgt"]) <= 76): continue else: bool = False break elif (dict["hcl"][0] == '#') and (len(dict["hcl"]) == 6): continue elif dict["ecl"] == "amb" or dict["ecl"] == "blu" or dict["ecl"] == "brn" or dict["ecl"] == "gry" or dict["ecl"] == "grn" or dict["ecl"] == "hzl" or dict["ecl"] == "oth": continue elif dict["pid"].count() == 9: continue else: bool = False break return bool part_two(file)