2021-01-04 13:59:11 +03:00
|
|
|
from fileio import open
|
|
|
|
let f = open('test/day4.in')
|
|
|
|
let lines = f.read().split('\n')[:-1]
|
|
|
|
|
|
|
|
def is_digits(val, cnt):
|
|
|
|
if len(val) != cnt:
|
|
|
|
return False
|
|
|
|
return not any([x not in '0123456789' for x in val])
|
|
|
|
|
|
|
|
def check_passport(passport):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Checking",passport)
|
2021-01-04 13:59:11 +03:00
|
|
|
let expected = ['byr','iyr','eyr','hgt','hcl','ecl','pid']
|
|
|
|
if not all([e in passport for e in expected]):
|
2021-01-04 17:33:43 +03:00
|
|
|
print('Missing expected value')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
#return 1
|
|
|
|
|
|
|
|
if not is_digits(passport['byr'], 4) or int(passport['byr']) < 1920 or int(passport['byr']) > 2002:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('Bad birth year')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if not is_digits(passport['iyr'], 4) or int(passport['iyr']) < 2010 or int(passport['iyr']) > 2020:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('Bad issue year')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if not is_digits(passport['eyr'], 4) or int(passport['eyr']) < 2020 or int(passport['eyr']) > 2030:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('Bad expire year')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if passport['hgt'][-2:] == 'cm':
|
|
|
|
if not is_digits(passport['hgt'][:-2], 3) or int(passport['hgt'][:-2]) < 150 or int(passport['hgt'][:-2]) > 193:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad height in cm')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
elif passport['hgt'][-2:] == 'in':
|
|
|
|
if not is_digits(passport['hgt'][:-2], 2) or int(passport['hgt'][:-2]) < 59 or int(passport['hgt'][:-2]) > 76:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad height in inches:', int(passport['hgt'][:-2]))
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
else:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad height generally')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if len(passport['hcl']) != 7 or passport['hcl'][0] != '#' or any([x not in '0123456789abcdef' for x in passport['hcl'][1:]]):
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad hair color')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if passport['ecl'] not in ['amb','blu','brn','gry','grn','hzl','oth']:
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad eye color')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
if not is_digits(passport['pid'], 9):
|
2021-01-04 17:33:43 +03:00
|
|
|
print('bad pid')
|
2021-01-04 13:59:11 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
let count = 0
|
|
|
|
let passport = {}
|
|
|
|
for line in lines:
|
|
|
|
if not len(line):
|
|
|
|
count += check_passport(passport)
|
|
|
|
passport = {}
|
|
|
|
continue
|
|
|
|
let elems = line.split(' ')
|
|
|
|
for elem in elems:
|
|
|
|
let s = elem.split(':', 1)
|
|
|
|
passport[s[0]] = s[1]
|
|
|
|
|
|
|
|
count += check_passport(passport)
|
2021-01-04 17:33:43 +03:00
|
|
|
print("count =", count)
|