def count(input,i):
return (input.a[i].find('byr:')>-1)+(input.a[i].find('iyr:')>-1)+(input.a[i].find('eyr:')>-1)+(input.a[i].find('hgt:')>-1)+(input.a[i].find('hcl:')>-1)+(input.a[i].find('ecl:')>-1)+(input.a[i].find('pid:')>-1)
def countall(input,i):
return (input.a[i].find('byr:')>-1)+(input.a[i].find('iyr:')>-1)+(input.a[i].find('eyr:')>-1)+(input.a[i].find('hgt:')>-1)+(input.a[i].find('hcl:')>-1)+(input.a[i].find('ecl:')>-1)+(input.a[i].find('pid:')>-1)+(input.a[i].find('cid:')>-1)
def validate(string):
list=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
list2=['amb','blu','brn','gry','grn','hzl','oth']
list3=['0','1','2','3','4','5','6','7','8','9']
valid=0
if (string.find('byr:')>-1):
valid= 1920<=int(string.replace('byr:', ''))<=2002
if (string.find('iyr:')>-1):
valid= 2010<=int(string.replace('iyr:', ''))<=2020
if (string.find('eyr:')>-1):
valid= 2020<=int(string.replace('eyr:', ''))<=2030
if (string.find('hgt:')>-1):
string=string.replace('hgt:', '')
if (string.find('cm')>-1):
valid= 150<=int(string.replace('cm', ''))<=193
if (string.find('in')>-1):
valid= 59<=int(string.replace('in', ''))<=76
if (string.find('hcl:#')>-1):
string=string.replace('hcl:#', '')
counter=0
for char in string:
counter+=char in list
valid= counter==6
if (string.find('ecl:')>-1):
string=string.replace('ecl:', '')
valid= string in list2
if (string.find('pid:')>-1):
string=string.replace('pid:', '')
counter=0
for char in string:
counter+=char in list3
valid= counter==9
return valid
#Part 1
import numpy as np
import pandas as pd
input=pd.read_csv('C:/Users/z003kpaw/Desktop/input1.txt',names=('a'),skip_blank_lines=False,keep_default_na=False)
valid=0
counter=0
for i in range(len(input)):
counter+=count(input,i)
if (countall(input,i)==0) | (i==len(input)-1): #If we got to the end of one or end of file
if counter==7:
valid+=1
counter=0
print (valid)
#Part 2
input=pd.read_csv('C:/Users/z003kpaw/Desktop/input1.txt',names=('a','b','c','d','e','f','g','h'),skip_blank_lines=False,keep_default_na=False,delim_whitespace=True)
valid=0
counter=0
for i in range(len(input)):
counter+=validate(input.a[i])+validate(input.b[i])+validate(input.c[i])+validate(input.d[i])+validate(input.e[i])+validate(input.f[i])+validate(input.g[i])+validate(input.h[i])
if (countall(input,i)==0) | (i==len(input)-1): #If we got to the end of one or end of file
if counter==7:
valid+=1
counter=0
print(valid)