| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- file = open('input.txt','r')
- tmp = file.readlines()
- count = 0
- items = []
- items.append('')
- for t in tmp:
- #print(str(count))
- if t == '\n':
- #print('newline')
- items.append('')
- count += 1
- else:
- #print(t)
- items[count] += ' '+t
- valid = 0
- def isvalid(item):
- tmp = item.strip().replace('\n','').split(' ')
- vals = []
- for t in tmp:
- vals.append(t.split(':'))
- for v in vals:
- print(v)
- if v[0] == 'byr':
- val = int(v[1])
- if val > 2002 or val < 1920:
- print('byr invlid')
- return False
- if v[0] == 'iyr':
- val = int(v[1])
- if val > 2020 or val < 2010:
- print('iyr invlid')
- return False
- if v[0] == 'eyr':
- val = int(v[1])
- if val > 2030 or val < 2020:
- print('eyr invlid')
- return False
- if v[0] == 'hgt':
- unit = v[1][-2:]
- if unit not in ['cm','in']:
- print('hgt wrong unit')
- return False
- val = int(v[1].replace(unit,''))
- if unit == 'in':
- if val > 76 or val < 59:
- print('hgt out of range')
- return False
- if unit == 'cm':
- if val > 193 or val < 150:
- print('hgt out of range')
- return False
- if v[0] == 'hcl':
- if len(v[1]) != 7:
- print('hcl to short')
- return False
- if v[1][1] == '#':
- print('hcl wrong format')
- return False
- for c in v[1].replace('#',''):
- if c not in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']:
- print('hcl unexpected char '+c)
- return False
- if v[0] == 'ecl':
- if len(v[1]) != 3:
- print('ecl to short')
- return False
- if v[1] not in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
- print('ecl undefined value')
- return False
- if v[0] == 'pid':
- if len(v[1]) != 9 or not v[1].isnumeric():
- print('pid invaldi')
- return False
- return True
- for i in items:
- if 'byr' not in i:
- continue
- if 'iyr' not in i:
- continue
- if 'hgt' not in i:
- continue
- if 'hcl' not in i:
- continue
- if 'ecl' not in i:
- continue
- if 'pid' not in i:
- continue
- if 'eyr' not in i:
- continue
- if isvalid(i):
- valid += 1
- print(str(valid))
|