entries = []
latest={}

fieldctr=0

good=0
ok=0
bad=0

def validate(entry):
    if 'byr' not in entry or 'iyr' not in entry or 'eyr' not in entry or 'hgt' not in entry or 'hcl' not in entry or 'ecl' not in entry or 'pid' not in entry:
        return False


    byr=int(entry['byr'])
    iyr=int(entry['iyr'])
    eyr=int(entry['eyr'])

    if byr<1920 or byr>2002:
        return False
    if iyr<2010 or iyr>2020:
        return False
    if eyr<2020 or eyr>2030:
        return False

    hgt=entry['hgt']

    if hgt.find("cm") >= 0:
        hgt=int(hgt.split("cm")[0])
        if hgt < 150 or hgt > 193:
            return False
    elif hgt.find("in") >= 0:
        hgt=int(hgt.split("in")[0])
        if hgt < 59 or hgt > 76: 
            return False
    else:
        return False

    hcl=entry['hcl']
    print(hcl)
    if hcl[0] != "#" or len(hcl)!=7:
        print("BAD")
        return False

    ecl=entry['ecl']
    if ecl!="amb" and ecl!="blu" and ecl!="brn" and ecl!="gry" and ecl!="grn" and ecl!="hzl" and ecl!="oth":
        return False

    pid=entry['pid']
    if len(pid)!=9 or pid.isnumeric() == False:
        return False

    return True

data=open("./in","r")
for l in data:
    if l == "\n":
        if fieldctr == 8:
            good += 1
        elif fieldctr == 7 and 'cid' not in latest:
            ok += 1
        else:
            bad +=1


        entries.append(latest);
        latest={}
        fieldctr=0
    else:
        fields=l.split(' ');
        for f in fields:
            fs=f.split(":")

            key=fs[0]
            value=fs[1]
            value=value.split("\n")
            value=value[0]
            
            latest[key]=value
            fieldctr+=1
data.close()

print("Good {} OK {} Both {}".format(good,ok,good+ok))
