kuroko/test/day4.krk

70 lines
2.1 KiB
Python
Raw Normal View History

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):
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]):
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:
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:
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:
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:
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:
print('bad height in inches:', int(passport['hgt'][:-2]))
2021-01-04 13:59:11 +03:00
return 0
else:
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:]]):
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']:
print('bad eye color')
2021-01-04 13:59:11 +03:00
return 0
if not is_digits(passport['pid'], 9):
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)
print("count =", count)