107 lines
2.3 KiB
Python
107 lines
2.3 KiB
Python
import re
|
|
import sys
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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(":")
|
|
if key_value[0] != "cid":
|
|
dict[key_value[0]] = key_value[1]
|
|
|
|
|
|
|
|
|
|
try:
|
|
if (int(dict["byr"]) >= 1920) and (int(dict["byr"]) <= 2002):
|
|
None
|
|
else:
|
|
bool = False
|
|
if (int(dict["iyr"]) >= 2010) and (int(dict["iyr"]) <= 2020):
|
|
None
|
|
else:
|
|
bool = False
|
|
if (int(dict["eyr"]) >= 2020) and (int(dict["eyr"]) <= 2030):
|
|
None
|
|
else:
|
|
|
|
bool = False
|
|
if ("cm" in dict["hgt"]) or ("in" in dict["hgt"]):
|
|
if "cm" in dict["hgt"]:
|
|
if (int(dict["hgt"].replace("cm","")) >= 150) and (int(dict["hgt"].replace("cm","")) <= 193):
|
|
None
|
|
elif "in" in dict["hgt"]:
|
|
if (int(dict["hgt"].replace("in","")) >= 59) and (int(dict["hgt"].replace("in","")) <= 76):
|
|
None
|
|
else:
|
|
bool = False
|
|
if (dict["hcl"][0] == '#') and (len(dict["hcl"]) == 7):
|
|
None
|
|
else:
|
|
bool = False
|
|
|
|
if 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":
|
|
None
|
|
else:
|
|
|
|
bool = False
|
|
if len(dict["pid"]) == 9:
|
|
None
|
|
else:
|
|
|
|
bool = False
|
|
|
|
except:
|
|
bool = False
|
|
return bool
|
|
|
|
|
|
part_one(file) # 222
|
|
part_two(file) # 140 |