64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import re
|
||
|
import sys
|
||
|
|
||
|
L = []
|
||
|
with open(sys.argv[1], 'r') as f:
|
||
|
L = f.readlines()
|
||
|
L = [x.strip() for x in L]
|
||
|
|
||
|
|
||
|
ACCEPTED_FIELDS = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'}
|
||
|
|
||
|
|
||
|
# part 1
|
||
|
j = 0
|
||
|
people = [0]
|
||
|
for i in L:
|
||
|
if i == '':
|
||
|
people.append(0)
|
||
|
continue
|
||
|
fields = i.split(' ')
|
||
|
for f in fields:
|
||
|
if f.split(':')[0] in ACCEPTED_FIELDS:
|
||
|
people[-1] += 1
|
||
|
valid = 0
|
||
|
for p in people:
|
||
|
if p == len(ACCEPTED_FIELDS):
|
||
|
valid += 1
|
||
|
print(valid)
|
||
|
|
||
|
# part 2
|
||
|
VALID_ECL = {'amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'}
|
||
|
|
||
|
j = 0
|
||
|
people = [{'valid': 0, 'byr': '', 'iyr': '', 'eyr': '', 'hgt': '', 'ecl': '', 'pid': ''}]
|
||
|
for i in L:
|
||
|
if i == '':
|
||
|
people.append({'valid': 0, 'byr': '', 'iyr': '', 'eyr': '', 'hgt': '', 'ecl': '', 'pid': ''})
|
||
|
continue
|
||
|
fields = i.split(' ')
|
||
|
for f in fields:
|
||
|
if f.split(':')[0] in ACCEPTED_FIELDS:
|
||
|
people[-1]['valid'] += 1
|
||
|
people[-1][f.split(':')[0]] = f.split(':')[1]
|
||
|
|
||
|
valid_people = []
|
||
|
for p in people:
|
||
|
if p['valid'] == len(ACCEPTED_FIELDS):
|
||
|
valid_people.append(p)
|
||
|
|
||
|
valid = 0
|
||
|
for v in valid_people:
|
||
|
if v['byr'].isdigit() and int(v['byr']) >= 1920 and int(v['byr']) <= 2002 and \
|
||
|
v['iyr'].isdigit() and int(v['iyr']) >= 2010 and int(v['iyr']) <= 2020 and \
|
||
|
v['eyr'].isdigit() and int(v['eyr']) >= 2020 and int(v['eyr']) <= 2030 and \
|
||
|
((v['hgt'].endswith('cm') and (int(v['hgt'].split('cm')[0]) >= 150 and int(v['hgt'].split('cm')[0]) <= 193)) or \
|
||
|
(v['hgt'].endswith('in') and (int(v['hgt'].split('in')[0]) >= 59 and int(v['hgt'].split('in')[0]) <= 76 ))) and \
|
||
|
(v['hcl'].startswith('#') and len(v['hcl'].split('#')[1]) == 6 and bool(re.match(r'^[0-9a-f]*$', v['hcl'].split('#')[1]))) and \
|
||
|
v['ecl'] in VALID_ECL and \
|
||
|
len(v['pid']) == 9 and bool(re.match(r'^[0-9]*$', v['pid'])):
|
||
|
valid += 1
|
||
|
print(valid)
|