-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.py
31 lines (22 loc) · 1.06 KB
/
day4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import re
input_text = open('day4_input.txt', 'r').read().split("\n\n")
keys = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
eye_cols = ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']
def passport_checker(input_text):
answer1 = answer2 = 0
for passport in input_text:
chunks = re.split('[\n ]', passport)
d = dict(chunk.split(":") for chunk in chunks)
if all(key in d for key in keys):
answer1 += 1
rules = [1920 <= int(d['byr']) <= 2002,
2010 <= int(d['iyr']) <= 2020,
2020 <= int(d['eyr']) <= 2030,
(d['hgt'].endswith('cm') and 150 <= int(d['hgt'][:-2]) <= 193) or (d['hgt'].endswith('in') and 59 <= int(d['hgt'][:-2]) <= 76),
bool(re.fullmatch(r'#[0-9a-f]{6}', d['hcl'])),
d['ecl'] in eye_cols,
bool(re.fullmatch(r'[0-9]{9}', d['pid']))]
if all(rules):
answer2 += 1
return(answer1, answer2)
print(passport_checker(input_text))